Interface Mock<TArgs, TReturns>

interface Mock<TArgs, TReturns> {
    new Mocknew (...args): TReturns;
    mock: MockContext<TArgs, TReturns>;
    getMockImplementation(): undefined | ((...args) => TReturns);
    getMockName(): string;
    mockClear(): this;
    mockImplementation(fn): this;
    mockImplementationOnce(fn): this;
    mockName(n): this;
    mockRejectedValue(obj): this;
    mockRejectedValueOnce(obj): this;
    mockReset(): this;
    mockResolvedValue(obj): this;
    mockResolvedValueOnce(obj): this;
    mockRestore(): void;
    mockReturnThis(): this;
    mockReturnValue(obj): this;
    mockReturnValueOnce(obj): this;
    withImplementation<T>(fn, cb): T extends Promise<unknown>
        ? Promise<Mock<TArgs, TReturns>>
        : Mock<TArgs, TReturns>;
    (...args): TReturns;
}

Type Parameters

  • TArgs extends any[] = any

  • TReturns = any

Hierarchy (view full)

  • Parameters

    Returns TReturns

Constructors

  • Parameters

    Returns TReturns

Properties

Current context of the mock. It stores information about all invocation calls, instances, and results.

Methods

  • Returns current mock implementation if there is one.

    If mock was created with vi.fn, it will consider passed down method as a mock implementation.

    If mock was created with vi.spyOn, it will return undefined unless a custom implementation was provided.

    Returns undefined | ((...args) => TReturns)

  • Use it to return the name given to mock with method .mockName(name).

    Returns string

  • Clears all information about every call. After calling it, all properties on .mock will return an empty state. This method does not reset implementations.

    It is useful if you need to clean up mock between different assertions.

    Returns this

  • Accepts a function that will be used as an implementation of the mock.

    Parameters

    Returns this

    Example

    const increment = vi.fn().mockImplementation(count => count + 1);
    expect(increment(3)).toBe(4);
  • Accepts a function that will be used as a mock implementation during the next call. Can be chained so that multiple function calls produce different results.

    Parameters

    Returns this

    Example

    const fn = vi.fn(count => count).mockImplementationOnce(count => count + 1);
    expect(fn(3)).toBe(4);
    expect(fn(3)).toBe(3);
  • Sets internal mock name. Useful to see the name of the mock if an assertion fails.

    Parameters

    • n: string

    Returns this

  • Accepts an error that will be rejected when async function is called.

    Parameters

    • obj: any

    Returns this

    Example

    const asyncMock = vi.fn().mockRejectedValue(new Error('Async error'))
    await asyncMock() // throws 'Async error'
  • Accepts a value that will be rejected during the next function call. If chained, every consecutive call will reject specified value.

    Parameters

    • obj: any

    Returns this

    Example

    const asyncMock = vi
    .fn()
    .mockResolvedValueOnce('first call')
    .mockRejectedValueOnce(new Error('Async error'))

    await asyncMock() // first call
    await asyncMock() // throws "Async error"
  • Does what mockClear does and makes inner implementation an empty function (returning undefined when invoked). This also resets all "once" implementations.

    This is useful when you want to completely reset a mock to the default state.

    Returns this

  • Accepts a value that will be resolved when async function is called.

    Parameters

    Returns this

    Example

    const asyncMock = vi.fn().mockResolvedValue(42)
    asyncMock() // Promise<42>
  • Accepts a value that will be resolved during the next function call. If chained, every consecutive call will resolve specified value.

    Parameters

    Returns this

    Example

    const myMockFn = vi
    .fn()
    .mockResolvedValue('default')
    .mockResolvedValueOnce('first call')
    .mockResolvedValueOnce('second call')

    // Promise<'first call'>, Promise<'second call'>, Promise<'default'>
    console.log(myMockFn(), myMockFn(), myMockFn())
  • Does what mockReset does and restores inner implementation to the original function.

    Note that restoring mock from vi.fn() will set implementation to an empty function that returns undefined. Restoring a vi.fn(impl) will restore implementation to impl.

    Returns void

  • Use this if you need to return this context from the method without invoking actual implementation.

    Returns this

  • Accepts a value that will be returned whenever the mock function is called.

    Parameters

    Returns this

  • Accepts a value that will be returned during the next function call. If chained, every consecutive call will return the specified value.

    When there are no more mockReturnValueOnce values to use, mock will fallback to the previously defined implementation if there is one.

    Parameters

    Returns this

    Example

    const myMockFn = vi
    .fn()
    .mockReturnValue('default')
    .mockReturnValueOnce('first call')
    .mockReturnValueOnce('second call')

    // 'first call', 'second call', 'default'
    console.log(myMockFn(), myMockFn(), myMockFn())

Generated using TypeDoc