In my angular application (v11.1.0), I am using Jest for UnitTests. Specifically, I utilize TestBed.inject to obtain a service instance within individual tests and spyOn their methods for testing purposes – whether they have been called or to mock the return values.
However, upon transitioning to TypeScript strict mode, the test fails consistently. Interestingly, rearranging the order of certain tests resolves the issue and everything runs smoothly. It appears that the mocked services are somehow interacting across different unit tests.
Although I attempted to use jest.resetAllMocks(), it did not offer a solution. Below is the code snippet used:
Unit Test
...
// Code snippet unchanged from original
...
Service Mock
...
// Code snippet unchanged from original
...
App Component
this.informationService.getAnnouncementsByType(AnnouncementType.BANNER)
.pipe(takeUntil(this.destroy$))
.subscribe(([currentLanguage, banners]) => {
if (banners?.length > 0) {
if (banners[0].title) {
this.maintenanceBannerTitle = banners[0].title[currentLanguage.key as keyof LanguageObject];
}
if (banners[0].text) {
this.maintenanceBannerMessage =
banners[0].text[currentLanguage.key as keyof LanguageObject];
}
}
});