Skip to content
GitHub

useMockFn

function in meocord/testing Since 4.2.0

useMockFn(factory: MockFnFactory): void

Makes every mock meocord/testing creates with the test runner's own mock function, such as Vitest's vi.fn.

Call it once in the runner's setup file. The runner then treats meocord's mocks as its own: its clearMocks and mockReset config and vi.clearAllMocks() reach them, its matchers read them, and its whole mock API is there. Without it, mocks are createMockFn's own, which jest's and Vitest's expect read too.

Parameters

NameTypeDescription
factoryMockFnFactory

The runner's mock function factory: vi.fn, jest.fn or bun's mock.

Throws

  • Error when a mock already exists from another factory, or when factory doesn't make a mock with jest's and Vitest's API.

Examples

TypeScript
// vitest.setup.ts
import { vi } from 'vitest'

useMockFn(vi.fn)