Gaëtan
Gaëtan

Reputation: 863

Angular Unit Test fail: No provider for WebSocketAPI

I have some of my unit tests failing in my Angular 10 app because of the following error:

Failed: R3InjectorError(DynamicTestModule)[WebSocketAPI -> WebSocketAPI]: NullInjectorError: No provider for WebSocketAPI!

Usually this error means that I forgot an import in one of my tests files, but I don't know what to import exactly to fix the error this time. I tried this:

import { WebSocketAPI } from './service/WebSocketService';

But it did not work, and Google research does not return anything interesting. I am not very familiar with Angular, does anyone know how to fix this error ?

Upvotes: 0

Views: 53

Answers (1)

Gaëtan
Gaëtan

Reputation: 863

I just needed to put WebSocketAPI as a provider in the beforeEach method of the test file:

beforeEach(async(() => {
    TestBed.configureTestingModule({
        declarations: [
            AppComponent
        ],
        imports: [
            RouterTestingModule,
            HttpClientTestingModule
        ],
        providers: [
            WebSocketAPI
        ]
    }).compileComponents();
}));

Upvotes: 1

Related Questions