0
I am encountering issues with unit testing while using the ngx-translate library. Despite adding a provider for TranslateService, the tests keep asking for more providers, creating an endless loop of dependencies.
Specifically, I am trying to unit test my auth component, which relies on the translate service in its constructor.
Even after including TranslateService as a provider (as shown below), it continues to request additional dependencies from translate. It seems like there must be a different solution to this problem.
providers:[
TranslateService
]
Here is an image showcasing the error:
app.module
return new TranslateHttpLoader(http);
}
@NgModule({
declarations: [
AppComponent
],
imports: [
...
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
},
defaultLanguage: 'en'
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
It's worth noting that the sharedModule already exports the TranslateModule used across the app.
Shared Module
@NgModule({
imports: [
...,
TranslateModule,
],
exports: [
...,
TranslateModule
],
providers:[
]
})
export class SharedModule { }
The auth component is also part of a lazy module.
auth.module
@NgModule({
declarations: [
AuthComponent
],
imports: [
CommonModule,
AuthRoutingModule,
SharedModule
]
})
export class AuthModule { }
auth.component.ts
export class AuthComponent implements OnInit {
constructor(public translate: TranslateService)
}
Spec.ts
describe('AuthComponent', () => {
let component: AuthComponent;
let fixture: ComponentFixture<AuthComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports:[
HttpClientTestingModule,
SharedModule,
],
providers:[
TranslateService,
],
declarations: [ AuthComponent ],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(AuthComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
Any suggestions on resolving this issue?