Skip to content
GitHub

MockInstance

interface in meocord/testing Since 4.0.0

interface MockInstance<
  T extends (...args: any[]) => any = (...args: any[]) => any,
>

The mock API of a mock function: what it has recorded, and the methods that change what it does.

Every mock from meocord/testing has it, with the names jest and Vitest use, so a test written for either runner reads the same.

Members

_isMockFunction

readonly _isMockFunction: true

Marks the function as a mock for jest and Vitest, which check it through isMockFunction.

mock

readonly mock: MockState<T>

What the mock has recorded: its calls, their outcomes and their this.

mockReturnValue

mockReturnValue(value: CallResult<T>): MockedFunction<T>

Returns value from every call, until another implementation is set.

Parameters

NameTypeDescription
valueCallResult<T>

Returns

MockedFunction<T>

mockReturnValueOnce

mockReturnValueOnce(value: CallResult<T>): MockedFunction<T>

Returns value from the next call only.

Parameters

NameTypeDescription
valueCallResult<T>

Returns

MockedFunction<T>

mockResolvedValue

mockResolvedValue(value: Awaited<CallResult<T>>): MockedFunction<T>

Resolves every call to value.

Parameters

NameTypeDescription
valueAwaited<CallResult<T>>

Returns

MockedFunction<T>

mockResolvedValueOnce

mockResolvedValueOnce(value: Awaited<CallResult<T>>): MockedFunction<T>

Resolves the next call only to value.

Parameters

NameTypeDescription
valueAwaited<CallResult<T>>

Returns

MockedFunction<T>

mockRejectedValue

mockRejectedValue(value: unknown): MockedFunction<T>

Rejects every call with value.

Parameters

NameTypeDescription
valueunknown

Returns

MockedFunction<T>

mockRejectedValueOnce

mockRejectedValueOnce(value: unknown): MockedFunction<T>

Rejects the next call only with value.

Parameters

NameTypeDescription
valueunknown

Returns

MockedFunction<T>

mockImplementation

mockImplementation(fn: Implementation<T>): MockedFunction<T>

Runs fn for every call.

Parameters

NameTypeDescription
fnImplementation<T>

Returns

MockedFunction<T>

mockImplementationOnce

mockImplementationOnce(fn: Implementation<T>): MockedFunction<T>

Runs fn for the next call only; *Once implementations run in the order they were set.

Parameters

NameTypeDescription
fnImplementation<T>

Returns

MockedFunction<T>

mockClear

mockClear(): MockedFunction<T>

Forgets the recorded calls, keeping the implementation.

Returns

MockedFunction<T>

mockReset

mockReset(): MockedFunction<T>

Forgets the recorded calls, and puts back the implementation the mock was created with.

Returns

MockedFunction<T>

mockRestore

mockRestore(): MockedFunction<T>

Does what mockReset does: a mock from meocord/testing replaces no original to restore.

Returns

MockedFunction<T>

getMockName

getMockName(): string

The name the mock reports in an assertion's message.

Returns

string

mockName

mockName(name: string): MockedFunction<T>

Sets the name the mock reports in an assertion's message.

Parameters

NameTypeDescription
namestring

Returns

MockedFunction<T>

See also