Skip to content

[Bug]: Spurious type error when calling mockRejectedValue on mocks of certain overloaded functions #16174

@dmurvihill

Description

@dmurvihill

Version

30.4

Steps to reproduce

Consider a method like pg.Client.end. It has two overloads:

    end(): Promise<void>;
    end(callback: (err: Error) => void): void;

When using a mocked instance, one might want to mock resolved or rejected values:

test('mock pg', async () => {
  const client: jest.Mocked<pg.Client> = setUpMockClient();
  client.end.mockRejectedValueOnce(new Error('test error when closing client')) // Type error
  await expect(unitUnderTest(client)).rejects.not.toBeNull()
});

Sorry this isn't a fully working minimal reproduction; I hope it gets the point across well enough.

Expected behavior

mockRejectedValue and mockResolvedValue should work so long as any one overload of the mocked function returns a Promise.

Actual behavior

TS2345: Argument of type Error is not assignable to parameter of type never. And similar behavior with the mockResolvedValue family. The type inference seems to assume that the callback mode will be used. If you try to work around with mockReturnValue(Promise.reject(e)), then you get a similar error TS2345: Argument of type Promise<never> is not assignable to parameter of type void.

Additional context

No response

Environment

dolan@Dolans-MacBook-Pro code % npx envinfo --preset jest

  System:
    OS: macOS 15.7.3
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 24.11.0 - ~/.nvm/versions/node/v24.11.0/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v24.11.0/bin/yarn
    npm: 11.11.1 - ~/.nvm/versions/node/v24.11.0/bin/npm
    pnpm: 8.15.4 - ~/.nvm/versions/node/v24.11.0/bin/pnpm

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions