PokéRogue
    Preparing search index...

    Interface MockInstance<T>

    interface MockInstance<T extends Procedure | Constructable = Procedure> {
        mock: MockContext<T>;
        "[dispose]"(): void;
        getMockImplementation(): NormalizedProcedure<T> | undefined;
        getMockName(): string;
        mockClear(): this;
        mockImplementation(fn: NormalizedProcedure<T>): this;
        mockImplementationOnce(fn: NormalizedProcedure<T>): this;
        mockName(name: string): this;
        mockRejectedValue(error: unknown): this;
        mockRejectedValueOnce(error: unknown): this;
        mockReset(): this;
        mockResolvedValue(value: Awaited<MockReturnType<T>>): this;
        mockResolvedValueOnce(value: Awaited<MockReturnType<T>>): this;
        mockRestore(): void;
        mockReturnThis(): this;
        mockReturnValue(value: MockReturnType<T>): this;
        mockReturnValueOnce(value: MockReturnType<T>): this;
        withImplementation(
            fn: NormalizedProcedure<T>,
            cb: () => Promise<unknown>,
        ): Promise<MockInstance<T>>;
        withImplementation(fn: NormalizedProcedure<T>, cb: () => unknown): this;
    }

    Type Parameters

    Hierarchy (View Summary)

    Index

    Properties

    mock: MockContext<T>

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

    Methods

    • Returns void

    • Returns current permanent 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 NormalizedProcedure<T> | undefined

    • Use it to return the name assigned to the mock with the .mockName(name) method. By default, it will return vi.fn().

      Returns string

    • Clears all information about every call. After calling it, all properties on .mock will return to their initial state. This method does not reset implementations. It is useful for cleaning up mocks between different assertions.

      To automatically call this method before each test, enable the clearMocks setting in the configuration.

      Returns this

    • Accepts a function to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function.

      Parameters

      Returns this

      const increment = vi.fn().mockImplementation(count => count + 1);
      expect(increment(3)).toBe(4);
    • Accepts a function to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function. This method can be chained to produce different results for multiple function calls.

      When the mocked function runs out of implementations, it will invoke the default implementation set with vi.fn(() => defaultValue) or .mockImplementation(() => defaultValue) if they were called.

      Parameters

      Returns this

      const fn = vi.fn(count => count).mockImplementationOnce(count => count + 1);
      expect(fn(3)).toBe(4);
      expect(fn(3)).toBe(3);
    • Sets the internal mock name. This is useful for identifying the mock when an assertion fails.

      Parameters

      • name: string

      Returns this

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

      Parameters

      • error: unknown

      Returns this

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

      Parameters

      • error: unknown

      Returns this

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

      await asyncMock() // first call
      await asyncMock() // throws Error<'Async error'>
    • Does what mockClear does and resets inner implementation to the original function. This also resets all "once" implementations.

      Note that resetting a mock from vi.fn() will set implementation to an empty function that returns undefined. Resetting a mock from vi.fn(impl) will set implementation to impl. It is useful for completely resetting a mock to its default state.

      To automatically call this method before each test, enable the mockReset setting in the configuration.

      Returns this

    • Accepts a value that will be resolved when the async function is called. TypeScript will only accept values that match the return type of the original function.

      Parameters

      Returns this

      const asyncMock = vi.fn().mockResolvedValue(42)
      asyncMock() // Promise<42>
    • Accepts a value that will be resolved during the next function call. TypeScript will only accept values that match the return type of the original function. If chained, each consecutive call will resolve the specified value.

      Parameters

      Returns this

      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 original descriptors of spied-on objects.

      Returns void

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

      Returns this

    • Accepts a value that will be returned whenever the mock function is called. TypeScript will only accept values that match the return type of the original function.

      Parameters

      Returns this

      const mock = vi.fn()
      mock.mockReturnValue(42)
      mock() // 42
      mock.mockReturnValue(43)
      mock() // 43
    • Accepts a value that will be returned whenever the mock function is called. TypeScript will only accept values that match the return type of the original function.

      When the mocked function runs out of implementations, it will invoke the default implementation set with vi.fn(() => defaultValue) or .mockImplementation(() => defaultValue) if they were called.

      Parameters

      Returns this

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

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