While working on an application using Angular 8 and ngx-mqtt, I encountered an error when running the tests defined in the .spec.ts
files. The error message reads:
NullInjectorError: StaticInjectorError(DynamicTestModule)[InjectionToken NgxMqttServiceConfig]:
StaticInjectorError(Platform: core)[InjectionToken NgxMqttServiceConfig]:
NullInjectorError: No provider for InjectionToken NgxMqttServiceConfig!
error properties: Object({...})
at <Jasmine>
...
at ZoneDelegate.invoke (...)
The .spec.ts
file I am currently using is as follows:
describe("MoviesComponent", () => {
let component: MoviesComponent;
let fixture: ComponentFixture<MoviesComponent>;
TestBed.prepare([MoviesComponent]);
beforeEach(() => {
fixture = TestBed.createComponent(MoviesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it("should create", () => {
expect(component).toBeTruthy(); // <------- FAILS HERE
});
});
Within the .component.ts
file, I utilize MqttService imported from "ngx-mqtt" and injected through the constructor. Although the components function correctly, the tests are failing. I have minimal experience with .spec.ts
files, yet I suspect that the error could be related to a missing import or provider within the test file.
I attempted adding MqttServiceConfig
to the MoviesComponent.spec.ts like so:
beforeEach(() => {
fixture = TestBed.configureTestingModule({ imports: [MqttServiceConfig] }).createComponent(MoviesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
Additionally, I tried this approach after referencing this tutorial:
it('should create', async(inject([MqttServiceConfig], (myService: MqttServiceConfig) => ...
My struggle lies in not knowing where to import NgxMqttServiceConfig
from. It appears to be contained within MqttModule, yet it is not exposed. I also attempted using MqttModule
instead of MqttServiceConfig
, but to no avail. What am I overlooking?