Mock
.toHaveBeenCalledBefore()
Use .toHaveBeenCalledBefore
when checking if a Mock
was called before another Mock
.
test('calls mock1 before mock2', () => { const mock1 = jest.fn(); const mock2 = jest.fn(); mock1(); mock2(); mock1(); expect(mock1).toHaveBeenCalledBefore(mock2); });
Tests
.toHaveBeenCalledAfter()
Use .toHaveBeenCalledAfter
when checking if a Mock
was called after another Mock
.
test('calls mock1 after mock2', () => { const mock1 = jest.fn(); const mock2 = jest.fn(); mock2(); mock1(); mock2(); expect(mock1).toHaveBeenCalledAfter(mock2); });
Tests
.toHaveBeenCalledOnce()
Use .toHaveBeenCalledOnce
to check if a Mock
was called exactly one time.
test('passes only if mock was called exactly once', () => { const mock = jest.fn(); expect(mock).not.toHaveBeenCalled(); mock(); expect(mock).toHaveBeenCalledOnce(); });
Tests
.toHaveBeenCalledOnceWith()
Use .toHaveBeenCalledOnceWith
to check if a Mock
was called exactly one time with the expected value.
test('passes only if mock was called exactly once with the expected value', () => { const mock = jest.fn(); expect(mock).not.toHaveBeenCalled(); mock('hello'); expect(mock).toHaveBeenCalledOnceWith('hello'); });
Tests