You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran into some trouble this morning because I incorrectly setup my test. I provided a mock of my service in providers instead of componentProviders, so the real service was called during my tests.
The error message I received was something like:
NullInjectorError: R3InjectorError(DynamicTestModule)[SomeUnexpectedService -> SomeOtherService -> ThirdUnexpectedService -> FourthUnexpectedService -> FifthUnexpectedService]:
NullInjectorError: No provider for FifthUnexpectedService!
import'zone.js/dist/zone';import{Component,inject,Injectable}from'@angular/core';import{CommonModule}from'@angular/common';import{bootstrapApplication}from'@angular/platform-browser';
@Injectable()exportclassMyService{doSomething(): string{console.log('Calling real API');return'real';}}
@Component({selector: 'my-app',standalone: true,imports: [CommonModule],providers: [MyService],template: ` <p>Value is {{ value | json }}</p> `,})exportclassApp{constructor(privatereadonlymyService: MyService){}publicvalue=this.myService.doSomething();}bootstrapApplication(App);
import{CommonModule}from'@angular/common';import{Spectator}from'@ngneat/spectator';import{createComponentFactory,mockProvider}from'@ngneat/spectator/jest';import{of}from'rxjs';import{App,MyService}from'src/main';describe('App',()=>{letspectator: Spectator<App>;constcreateComponentWithCorrectProviderMock=createComponentFactory({component: App,shallow: true,componentProviders: [mockProvider(MyService,{doSomething: jest.fn().mockReturnValue('mocked'),}),],imports: [CommonModule],});constcreateComponentWithWrongProvider=createComponentFactory({component: App,shallow: true,providers: [mockProvider(MyService,{doSomething: jest.fn().mockReturnValue(of('mocked')),}),],imports: [CommonModule],});test('passing test',()=>{spectator=createComponentWithCorrectProviderMock();expect(spectator.component.value).toEqual('mocked');});test('failing test',()=>{spectator=createComponentWithWrongProvider();// This will use the real provider, because we incorrectly// use `providers`, instead of `componentProviders`.expect(spectator.component.value).toEqual('mocked');});});
Proposed solution
I think it would be very helpful if we were able to provide a hint to the developer here to check if their service is provided in providers instead of componentProviders.
Alternatives considered
I am not sure if this is possible with spectator, or if this would have to be done in Jest? I filed the issue here, because the providers vs componentProviders is declared in SpectatorOptions.
Do you want to create a pull request?
No
The text was updated successfully, but these errors were encountered:
Description
Hi everyone,
I ran into some trouble this morning because I incorrectly setup my test. I provided a mock of my service in
providers
instead ofcomponentProviders
, so the real service was called during my tests.The error message I received was something like:
Example:
Proposed solution
I think it would be very helpful if we were able to provide a hint to the developer here to check if their service is provided in
providers
instead ofcomponentProviders
.Alternatives considered
I am not sure if this is possible with spectator, or if this would have to be done in Jest? I filed the issue here, because the
providers
vscomponentProviders
is declared inSpectatorOptions
.Do you want to create a pull request?
No
The text was updated successfully, but these errors were encountered: