I am working on unit tests in my Angular application, I am using the TestBed method,
I am testing components, so each specification file looks like this
import... describe('AppComponent', () => { // Importing dependecies beforeEach(async(() => { TestBed.configureTestingModule({ imports : [RouterTestingModule , HttpModule , FormsModule ], declarations: [AppComponent ], providers: [AUTH_PROVIDERS ,UserService, SharedclientService, RouteNavigator, JwtHelper, ReloadTokenService, ShopService , EnvVarsService, ProfileService, LocalStorageService, ApiVersionInterceptor, ApiTrackingInterceptor, MonitoringService , { provide: 'LOCAL_STORAGE_SERVICE_CONFIG', useValue: userConfig } , TokenUtilService , HttpInterceptorService , { provide: InterceptableStoreFactory, useClass: InterceptableStoreFactoryMock },ReloadTokenEventService , InterceptableStoreFactory ] }).compileComponents(); })); // detecting changes every times beforeEach(() => { fixture = TestBed.createComponent(AppComponent); component = fixture.componentInstance; fixture.detectChanges(); }); // Test case 0 (compilation of the component) it('AppComponent is well defined', () => { expect(component).toBeDefined(); }); // Test case 1 it('test', () => { expect("1").toBe("1"); }); });
This testing approach crashes the entire test suite if the dependency import is small.
For example : in this test suite, it raises this error:
No provider for InterceptableStoreFactory ! It seems strange since I import this service from my providers (last)
This leads to an almost complete failure of all test cases when checking the import of devices - these are βbefore eachβ test cases.
We are looking for the best ideas for:
- "no service provider" problem (which has already been added to providers)
and for
- unit testBed is the best way to test