Соответствия matcher’ов в Jasmine и Jest
- and.callThrough() –> mockImplementation()
- and.callFake() –> mockImplementation()
- and.returnValue() –> mockReturnValue()
- and.spyOnProperty() –> spyOn()
- and.toHaveBeenCalledOnceWith() –> toHaveBeenCalledTimes(1)
- spyOn(…).and.callFake(() => {}) –> jest.spyOn(…).mockImplementation(() => {})
- jasmine.createSpy(‘name’) –> jest.fn()
- toBeTrue –> toBe(true)
- toBeFalse –> toBe(false)
toHaveBeenCalled() - это алиас для toBeCalled()
Jasmine createSpyObj в Jest
В Jasmine объект шпиона создается, используя функцию createSpyObj и передавая в него параметры имени класса и массива методов:
const serviceMock = createSpyObj('service', ['method_1', 'method_2', 'method_3', 'method_4', 'method_5']);
В Jest просто создается объект с ожидаемыми свойствами, а функция jest.fn() создает методы-шпионы:
const serviceMock = {
method_1: jest.fn(),
method_2: jest.fn(),
method_3: jest.fn(),
method_4: jest.fn(),
method_5: jest.fn()
};