Interface MockContext<TArgs, TReturns>

interface MockContext<TArgs, TReturns> {
    calls: TArgs[];
    instances: TReturns[];
    invocationCallOrder: number[];
    lastCall: undefined | TArgs;
    results: MockResult<TReturns>[];
}

Type Parameters

  • TArgs

  • TReturns

Properties

calls: TArgs[]

This is an array containing all arguments for each call. One item of the array is the arguments of that call.

Example

const fn = vi.fn()

fn('arg1', 'arg2')
fn('arg3')

fn.mock.calls === [
['arg1', 'arg2'], // first call
['arg3'], // second call
]
instances: TReturns[]

This is an array containing all instances that were instantiated when mock was called with a new keyword. Note that this is an actual context (this) of the function, not a return value.

invocationCallOrder: number[]

The order of mock's execution. This returns an array of numbers which are shared between all defined mocks.

Example

const fn1 = vi.fn()
const fn2 = vi.fn()

fn1()
fn2()
fn1()

fn1.mock.invocationCallOrder === [1, 3]
fn2.mock.invocationCallOrder === [2]
lastCall: undefined | TArgs

This contains the arguments of the last call. If spy wasn't called, will return undefined.

results: MockResult<TReturns>[]

This is an array containing all values that were returned from the function.

The value property contains the returned value or thrown error. If the function returned a promise, the value will be the resolved value, not the actual Promise, unless it was never resolved.

Example

const fn = vi.fn()
.mockReturnValueOnce('result')
.mockImplementationOnce(() => { throw new Error('thrown error') })

const result = fn()

try {
fn()
}
catch {}

fn.mock.results === [
{
type: 'return',
value: 'result',
},
{
type: 'throw',
value: Error,
},
]

Generated using TypeDoc