Skip to content

Latest commit

History

History
63 lines (46 loc) 路 1.53 KB

Mock.mdx

File metadata and controls

63 lines (46 loc) 路 1.53 KB

import { TestFile } from '../../src/components/CustomSandpack';

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();\n mock1(); mock2(); mock1();\n expect(mock1).toHaveBeenCalledBefore(mock2); });`}

.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();\n mock2(); mock1(); mock2();\n expect(mock1).toHaveBeenCalledAfter(mock2); });`}

.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();\n expect(mock).not.toHaveBeenCalled(); mock(); expect(mock).toHaveBeenCalledOnce(); });`}

.toHaveBeenCalledOnceWith()

Use .toHaveBeenCalledOnceWith to check if a Mock was called exactly one time with the expected values.

{`test('passes only if mock was called exactly once with the expected value', () => { const mock = jest.fn();\n expect(mock).not.toHaveBeenCalled(); mock('hello'); expect(mock).toHaveBeenCalledOnceWith('hello'); });`}