createMockFn
function in meocord/testing Since 4.0.0
createMockFn<
T extends (...args: any[]) => any = (...args: any[]) => any,
>(impl?: T): MockedFunction<T>Creates a mock function that jest's and Vitest's expect both read.
Use it for a function double in code that must not depend on one test runner, such as a shared test helper; in a
Vitest test, vi.fn() works as well. clearAllMocks and resetAllMocks reach it. Once
useMockFn registers a runner's function, it returns that runner's mock.
Parameters
| Name | Type | Description |
|---|---|---|
impl? | T | The implementation to run until another is set. |
Returns
MockedFunction<T>Examples
import { expect } from 'vitest'
const double = createMockFn((value: number) => value * 2)
expect(double(2)).toBe(4)
double.mockReturnValueOnce(99)
expect(double(2)).toBe(99)
expect(double).toHaveBeenCalledTimes(2)