Angular Jasmine Karma test when component has own module

Always remember that your TestBed.configureTestingModule() is there on your test to recreate your module the same way is created on the app.

Let’s assume that you have a structure like following:

 

- your.component

  - your-child1.component

  - your.child2.component

-your.pipe

-your.module

In your.module you will import and declare all your three components and the pipe:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { YourComponent }  from './path.to.component';
import { YourChild1Component } from './path.to.component';
import { YourChild2Component } from './path.to.component';
import { YourPipe } from './path.to.component';

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ YourComponent, YourChild1Component, YourChild2Component, YourPipe ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

 

Same way you should do in your test class for your tests to work, you import the components and the pipe and add them to your

TestBed.configureTestModule(declarations: [Your component, YourChild1Component, YourChild2Component, YourPipe])

 

Leave a Reply

Your email address will not be published. Required fields are marked *