Skip to content

Commit a09bd56

Browse files
committed
Fix type definitions for promises
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.
1 parent 999ee46 commit a09bd56

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- `[jest-mock]` Fix typings for `mockResolvedValue`, `mockResolvedValueOnce`, `mockRejectedValue` and `mockRejectedValueOnce`
8+
79
### Chore & Maintenance
810

911
### Performance

packages/jest-mock/src/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ namespace JestMock {
6363
mockReturnThis(): this;
6464
mockReturnValue(value: T): this;
6565
mockReturnValueOnce(value: T): this;
66-
mockResolvedValue(value: T): this;
67-
mockResolvedValueOnce(value: T): this;
68-
mockRejectedValue(value: T): this;
69-
mockRejectedValueOnce(value: T): this;
66+
mockResolvedValue(value: Unpromisify<T>): this;
67+
mockResolvedValueOnce(value: Unpromisify<T>): this;
68+
mockRejectedValue(value: unknown): this;
69+
mockRejectedValueOnce(value: unknown): this;
7070
}
7171
}
7272

73+
type Unpromisify<T> = T extends Promise<infer R> ? R : never;
74+
7375
/**
7476
* Possible types of a MockFunctionResult.
7577
* 'return': The call completed by returning normally.
@@ -661,20 +663,20 @@ class ModuleMockerClass {
661663
// next function call will return this value or default return value
662664
f.mockImplementationOnce(() => value);
663665

664-
f.mockResolvedValueOnce = (value: T) =>
665-
f.mockImplementationOnce(() => Promise.resolve(value));
666+
f.mockResolvedValueOnce = (value: Unpromisify<T>) =>
667+
f.mockImplementationOnce(() => Promise.resolve(value as T));
666668

667-
f.mockRejectedValueOnce = (value: T) =>
669+
f.mockRejectedValueOnce = (value: unknown) =>
668670
f.mockImplementationOnce(() => Promise.reject(value));
669671

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

674-
f.mockResolvedValue = (value: T) =>
675-
f.mockImplementation(() => Promise.resolve(value));
676+
f.mockResolvedValue = (value: Unpromisify<T>) =>
677+
f.mockImplementation(() => Promise.resolve(value as T));
676678

677-
f.mockRejectedValue = (value: T) =>
679+
f.mockRejectedValue = (value: unknown) =>
678680
f.mockImplementation(() => Promise.reject(value));
679681

680682
f.mockImplementationOnce = (

test-types/top-level-jest-namespace.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import {expectError, expectType} from 'mlh-tsd';
1111
//eslint-disable-next-line import/no-extraneous-dependencies
1212
import {jest} from '@jest/globals';
13+
import JestMock = require('jest-mock');
1314

1415
expectType<void>(jest.addMatchers({}));
1516
expectType<typeof jest>(jest.autoMockOff());
@@ -37,6 +38,26 @@ expectType<typeof jest>(jest.resetModuleRegistry());
3738
expectType<typeof jest>(jest.resetModules());
3839
expectType<typeof jest>(jest.isolateModules(() => {}));
3940
expectType<typeof jest>(jest.retryTimes(3));
41+
expectType<JestMock.Mock<Promise<string>, []>>(
42+
jest
43+
.fn(() => Promise.resolve('string value'))
44+
.mockResolvedValueOnce('A string, not a Promise'),
45+
);
46+
expectType<JestMock.Mock<Promise<string>, []>>(
47+
jest
48+
.fn(() => Promise.resolve('string value'))
49+
.mockResolvedValue('A string, not a Promise'),
50+
);
51+
expectType<JestMock.Mock<Promise<string>, []>>(
52+
jest
53+
.fn(() => Promise.resolve('string value'))
54+
.mockRejectedValueOnce(new Error('An error, not a string')),
55+
);
56+
expectType<JestMock.Mock<Promise<string>, []>>(
57+
jest
58+
.fn(() => Promise.resolve('string value'))
59+
.mockRejectedValue(new Error('An error, not a string')),
60+
);
4061

4162
expectType<void>(jest.runAllImmediates());
4263
expectType<void>(jest.runAllTicks());

0 commit comments

Comments
 (0)