Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma keeps asking for more imports that have nothing to do with the tested component

I'm testing my DashboardComponent, this is my beforeEach block:

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        FormsModule,
        routing
      ],
      declarations: [
        DashboardComponent,
        Ellipsis,
        FavouritesComponent,
        FormatDuration,
        LoginComponent,
        SearchComponent
      ],
    });
    this.fixture = TestBed.createComponent(DashboardComponent);
  });

It already has a few imports that have nothing to do with the dashboard (like LoginComponent). But it keeps asking for more imports:

Error: Component **** is not part of any NgModule or the module has not been imported into your module.

Everything in app.module.ts is present so that's not the problem.

What causes this kind of behaviour and how can I resolve it?

like image 324
stijn.aerts Avatar asked Oct 24 '25 19:10

stijn.aerts


1 Answers

Add AppModule and RouterTestingModule to imports of the object you pass to TestBed.configureTestingModule().

Not sure if you still need to add {provide: APP_BASE_HREF, useValue: '/'} when RouterTestingModule is imported.

See also this PR for the upcoming testing docs https://github.com/angular/angular.io/pull/2198/files

like image 105
Günter Zöchbauer Avatar answered Oct 26 '25 09:10

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!