In Angular, using Jest we have 2 tests that test a method on a component class:
describe('checkEmailStatus', () => {
it('set VERIFIED page design when email verification succeeds', async () => {
jest.spyOn(authService, 'checkEmailVerification');
await expect(component.checkEmailStatus()).resolves.toEqual(undefined);
expect(authService.checkEmailVerification).toBeCalledTimes(1);
expect(component.pageDesign.key).toBe('verified');
});
it('set ERROR page design when email verification fails', async () => {
const checkEmail = jest.spyOn(authService, 'checkEmailVerification');
checkEmail.mockImplementation(() => {
return Promise.reject(false);
});
await expect(component.checkEmailStatus()).resolves.toEqual(undefined);
expect(authService.checkEmailVerification).toBeCalledTimes(1);
expect(component.pageDesign.key).toBe('error');
});
});
These tests have been running fine for a month. Nothing about this component has changed and neither have we changed Jest version (25.2.7) yet now the 2nd test complains that the method was called 3 times.
If I comment out the first test, the 2nd tests passes.
It seems that the first test is not tearing down correctly - is there something I need to do to force that? (I tried using the done() callback, but it made no difference)
UPDATE
This is the method under test:
async checkEmailStatus(): Promise<void> {
this.isLoading = true;
try {
await this.authService.checkEmailVerification('');
this.setPageDesign('verified');
this.isLoading = false;
} catch (error) {
this.setPageDesign('error');
this.isLoading = false;
}
}
This is the stubbed authService:
import {Observable, BehaviorSubject, of} from 'rxjs';
import {switchMap} from 'rxjs/operators';
import {mockUsers} from '../../../../mocks/user.mock';
// tslint:disable-next-line: completed-docs
function initStub() {
const userId$ = new BehaviorSubject<string>(null);
return {
userId$,
checkEmailVerification(): Promise<boolean> {
return Promise.resolve(true);
}
};
}
export const authServiceStub = initStub();
Aucun commentaire:
Enregistrer un commentaire