diff --git a/CHANGELOG.md b/CHANGELOG.md index fd028883f897..110c259f9220 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[feat(@jest/environment, jest-runtime)]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) - `[@jest/fake-timers]` Add `jest.now()` to return the current fake clock time ([#13244](https://github.com/facebook/jest/pull/13244), [13246](https://github.com/facebook/jest/pull/13246)) ### Fixes diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 5f63b7efcb77..60b8e7ac034d 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -416,9 +416,7 @@ _Note It is recommended to use [`jest.mock()`](#jestmockmodulename-factory-optio Returns the actual module instead of a mock, bypassing all checks on whether the module should receive a mock implementation or not. -Example: - -```js +```js tab jest.mock('../myModule', () => { // Require the original module to not be mocked... const originalModule = jest.requireActual('../myModule'); @@ -426,7 +424,25 @@ jest.mock('../myModule', () => { return { __esModule: true, // Use it when dealing with esModules ...originalModule, - getRandom: jest.fn().mockReturnValue(10), + getRandom: jest.fn(() => 10), + }; +}); + +const getRandom = require('../myModule').getRandom; + +getRandom(); // Always returns 10 +``` + +```ts tab +jest.mock('../myModule', () => { + // Require the original module to not be mocked... + const originalModule = + jest.requireActual('../myModule'); + + return { + __esModule: true, // Use it when dealing with esModules + ...originalModule, + getRandom: jest.fn(() => 10), }; }); diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index a683bba060cf..c52bf740c508 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -205,7 +205,7 @@ export interface Jest { getRandom(); // Always returns 10 ``` */ - requireActual: (moduleName: string) => unknown; + requireActual(moduleName: string): T; /** * Wraps types of the `source` object and its deep members with type definitions * of Jest mock function. Pass `{shallow: true}` option to disable the deeply @@ -216,7 +216,7 @@ export interface Jest { * Returns a mock module instead of the actual module, bypassing all checks * on whether the module should be required normally or not. */ - requireMock: (moduleName: string) => unknown; + requireMock(moduleName: string): T; /** * Resets the state of all mocks. Equivalent to calling `.mockReset()` on * every mocked function. diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index cf0521e324db..d3b6d59fefd3 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -2129,8 +2129,8 @@ export default class Runtime { mock, mocked, now: () => _getFakeTimers().now(), - requireActual: this.requireActual.bind(this, from), - requireMock: this.requireMock.bind(this, from), + requireActual: moduleName => this.requireActual(from, moduleName), + requireMock: moduleName => this.requireMock(from, moduleName), resetAllMocks, resetModules, restoreAllMocks, diff --git a/packages/jest-types/__typetests__/jest.test.ts b/packages/jest-types/__typetests__/jest.test.ts index 53627054f652..7bda6906d508 100644 --- a/packages/jest-types/__typetests__/jest.test.ts +++ b/packages/jest-types/__typetests__/jest.test.ts @@ -118,9 +118,15 @@ expectType( ); expectType(jest.requireActual('./pathToModule')); +expectType<{some: 'module'}>( + jest.requireActual<{some: 'module'}>('./pathToModule'), +); expectError(jest.requireActual()); expectType(jest.requireMock('./pathToModule')); +expectType<{some: 'module'}>( + jest.requireMock<{some: 'module'}>('./pathToModule'), +); expectError(jest.requireMock()); expectType(jest.resetModules());