I am working on a unit test in angular regarding singleton service. I have a UserService that is provided in the root module is a singleton in the application and I want to write a unit test to check if the instance of UserService is the same throughout the application. I can do something like below but it would check component and Testbed instance. Is there any way I can write one unit test for all the feature modules to check if we have the same instance? Appreciate any help.
import { TestBed, ComponentFixture, inject } from '@angular/core/testing';
import { LoginComponent } from './login.component';
import { AuthService } from "./auth.service";
class MockAuthService extends AuthService {
isAuthenticated() {
return 'Mocked';
}
}
describe('Component: Login', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
let testBedService: AuthService;
let componentService: AuthService;
beforeEach(() => {
// refine the test module by declaring the test component
TestBed.configureTestingModule({
declarations: [LoginComponent],
providers: [AuthService]
});
// Configure the component with another set of Providers
TestBed.overrideComponent(
LoginComponent,
{ set: { providers: [{ provide: AuthService, useClass: MockAuthService }] } }
);
// create component and test fixture
fixture = TestBed.createComponent(LoginComponent);
// get test component from the fixture
component = fixture.componentInstance;
// AuthService provided to the TestBed
testBedService = TestBed.get(AuthService);
// AuthService provided by Component, (should return MockAuthService)
componentService = fixture.debugElement.injector.get(AuthService);
});
it('Service injected via inject(...) and TestBed.get(...) should be the same instance',
inject([AuthService], (injectService: AuthService) => {
expect(injectService).toBe(testBedService);
})
);
});