Skip to content

Commit

Permalink
Fix type definitions for promises (#10600)
Browse files Browse the repository at this point in the history
When calling `mockResolvedValue` (-`Once`), the argument should be
the expected return value unwrapped from its Promise. Likewise,
when mocking a rejected value, the passed argument needs not
necessarily be of the same type as the expected successful return
value.
  • Loading branch information
Vinnl authored Oct 6, 2020
1 parent 999ee46 commit 911b74e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-mock]` Fix typings for `mockResolvedValue`, `mockResolvedValueOnce`, `mockRejectedValue` and `mockRejectedValueOnce`

### Chore & Maintenance

### Performance
Expand Down
22 changes: 12 additions & 10 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ namespace JestMock {
mockReturnThis(): this;
mockReturnValue(value: T): this;
mockReturnValueOnce(value: T): this;
mockResolvedValue(value: T): this;
mockResolvedValueOnce(value: T): this;
mockRejectedValue(value: T): this;
mockRejectedValueOnce(value: T): this;
mockResolvedValue(value: Unpromisify<T>): this;
mockResolvedValueOnce(value: Unpromisify<T>): this;
mockRejectedValue(value: unknown): this;
mockRejectedValueOnce(value: unknown): this;
}
}

type Unpromisify<T> = T extends Promise<infer R> ? R : never;

/**
* Possible types of a MockFunctionResult.
* 'return': The call completed by returning normally.
Expand Down Expand Up @@ -661,20 +663,20 @@ class ModuleMockerClass {
// next function call will return this value or default return value
f.mockImplementationOnce(() => value);

f.mockResolvedValueOnce = (value: T) =>
f.mockImplementationOnce(() => Promise.resolve(value));
f.mockResolvedValueOnce = (value: Unpromisify<T>) =>
f.mockImplementationOnce(() => Promise.resolve(value as T));

f.mockRejectedValueOnce = (value: T) =>
f.mockRejectedValueOnce = (value: unknown) =>
f.mockImplementationOnce(() => Promise.reject(value));

f.mockReturnValue = (value: T) =>
// next function call will return specified return value or this one
f.mockImplementation(() => value);

f.mockResolvedValue = (value: T) =>
f.mockImplementation(() => Promise.resolve(value));
f.mockResolvedValue = (value: Unpromisify<T>) =>
f.mockImplementation(() => Promise.resolve(value as T));

f.mockRejectedValue = (value: T) =>
f.mockRejectedValue = (value: unknown) =>
f.mockImplementation(() => Promise.reject(value));

f.mockImplementationOnce = (
Expand Down
21 changes: 21 additions & 0 deletions test-types/top-level-jest-namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import {expectError, expectType} from 'mlh-tsd';
//eslint-disable-next-line import/no-extraneous-dependencies
import {jest} from '@jest/globals';
import type {Mock} from 'jest-mock';

expectType<void>(jest.addMatchers({}));
expectType<typeof jest>(jest.autoMockOff());
Expand Down Expand Up @@ -37,6 +38,26 @@ expectType<typeof jest>(jest.resetModuleRegistry());
expectType<typeof jest>(jest.resetModules());
expectType<typeof jest>(jest.isolateModules(() => {}));
expectType<typeof jest>(jest.retryTimes(3));
expectType<Mock<Promise<string>, []>>(
jest
.fn(() => Promise.resolve('string value'))
.mockResolvedValueOnce('A string, not a Promise'),
);
expectType<Mock<Promise<string>, []>>(
jest
.fn(() => Promise.resolve('string value'))
.mockResolvedValue('A string, not a Promise'),
);
expectType<Mock<Promise<string>, []>>(
jest
.fn(() => Promise.resolve('string value'))
.mockRejectedValueOnce(new Error('An error, not a string')),
);
expectType<Mock<Promise<string>, []>>(
jest
.fn(() => Promise.resolve('string value'))
.mockRejectedValue(new Error('An error, not a string')),
);

expectType<void>(jest.runAllImmediates());
expectType<void>(jest.runAllTicks());
Expand Down

0 comments on commit 911b74e

Please sign in to comment.