Jest Support

By default, Spectator uses Jasmine for creating spies. If you are using Jest as test framework instead, you can let Spectator create Jest-compatible spies.

Just import one of the following functions from @netbasal/spectator/jest (instead of @netbasal/spectator), and it will use Jest instead of Jasmine.

  • createTestComponentFactory()

  • createHostComponentFactory()

  • createService()

  • createHTTPFactory()

  • mockProvider()

import { AuthService } from './auth.service';
import { DateService } from './date.service';
import { createService } from '@netbasal/spectator/jest';
describe('AuthService', () => {
const spectator = createService({
service: AuthService,
mocks: [DateService]
});
it('should not be logged in', () => {
const dateService = spectator.get<DateService>(DateService);
dateService.isExpired.mockReturnValue(true);
expect(spectator.service.isLoggedIn()).toBeFalsy();
});
it('should be logged in', () => {
const dateService = spectator.get<DateService>(DateService);
dateService.isExpired.mockReturnValue(false);
expect(spectator.service.isLoggedIn()).toBeTruthy();
});
});

You can find more examples in the playground.