When working with Jest, you have the ability to mock out and spy on function calls within a function using functionalities like jest.spyOn and jest.fn() with .toHaveBeenCalledTimes(1) etc. However, in Spock framework testing, you can conclude your unit test by adding:
0 * _ // don't allow any other interaction
Is there a similar approach in Jest for achieving this restriction?
For example:
export default class Service {
public startAllWorkers(): void {
const processClient: ProcessClient = new ProcessClient();
const processMonitor: ProcessMonitor = new ProcessMonitor();
const defaultValue: number = 10;
processClient.runClient(defaultValue);
processMonitor.runMonitor('pling')
}
}
describe('Service test', () => {
let service: Service;
beforeEach(() => {
service = new Service();
ProcessClient.prototype.runClient = jest.fn()
ProcessMonitor.prototype.runMonitor = jest.fn()
});
it('should only call specific methods', () => {
const spyService = jest.spyOn(service, 'startAllWorkers');
service.startAllWorkers();
expect(spyService).toHaveBeenCalledTimes(1);
expect(ProcessClient.prototype.runClient).toHaveBeenCalledTimes(1);
expect(ProcessMonitor.prototype.runMonitor).toHaveBeenCalledTimes(1);
// expect no other interactions inside service method
});
})