From d608bb3b77d2eea8d42f6f9643a32040ecdcf560 Mon Sep 17 00:00:00 2001 From: sandrina-p Date: Wed, 22 May 2019 13:34:57 +0100 Subject: [PATCH 1/5] Update JestObjectAPI.md - Add example to jest.requireActual --- docs/JestObjectAPI.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 9a1e653014fc..1eef66de74b7 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -321,6 +321,25 @@ _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 +jest.mock('../myModule', () => { + // Require the original module to not be mocked... + const originalModule = jest.requireActual(moduleName); + + return { + __esModule: true, // Use it when dealing with esModules + ...originalModule, + getRandom: jest.fn().mockReturnValue(10), + }; +}); + +const getRandom = require('../myModule').getRandom; + +getRandom(); // Always returns 10 +``` + ### `jest.requireMock(moduleName)` Returns a mock module instead of the actual module, bypassing all checks on whether the module should be required normally or not. From 9d531b44606e61afa78e77359f48c6faa6407e48 Mon Sep 17 00:00:00 2001 From: sandrina-p Date: Wed, 22 May 2019 13:35:12 +0100 Subject: [PATCH 2/5] Update JestObjectAPI on versioned docs --- .../version-22.x/JestObjectAPI.md | 19 +++++++++++++++++++ .../version-23.x/JestObjectAPI.md | 19 +++++++++++++++++++ .../version-24.0/JestObjectAPI.md | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/website/versioned_docs/version-22.x/JestObjectAPI.md b/website/versioned_docs/version-22.x/JestObjectAPI.md index 7493f2c70715..8fbe46f7cf85 100644 --- a/website/versioned_docs/version-22.x/JestObjectAPI.md +++ b/website/versioned_docs/version-22.x/JestObjectAPI.md @@ -444,6 +444,25 @@ This is useful for scenarios such as one where the module being tested schedules Returns the actual module instead of a mock, bypassing all checks on whether the module should receive a mock implementation or not. +Example: + +```js +jest.mock('../myModule', () => { + // Require the original module to not be mocked... + const originalModule = jest.requireActual(moduleName); + + return { + __esModule: true, // Use it when dealing with esModules + ...originalModule, + getRandom: jest.fn().mockReturnValue(10), + }; +}); + +const getRandom = require('../myModule').getRandom; + +getRandom(); // Always returns 10 +``` + ### `jest.requireMock(moduleName)` Returns a mock module instead of the actual module, bypassing all checks on whether the module should be required normally or not. diff --git a/website/versioned_docs/version-23.x/JestObjectAPI.md b/website/versioned_docs/version-23.x/JestObjectAPI.md index 8ad63e3a6f27..52b2b6fd19dd 100644 --- a/website/versioned_docs/version-23.x/JestObjectAPI.md +++ b/website/versioned_docs/version-23.x/JestObjectAPI.md @@ -460,6 +460,25 @@ This is useful for scenarios such as one where the module being tested schedules Returns the actual module instead of a mock, bypassing all checks on whether the module should receive a mock implementation or not. +Example: + +```js +jest.mock('../myModule', () => { + // Require the original module to not be mocked... + const originalModule = jest.requireActual(moduleName); + + return { + __esModule: true, // Use it when dealing with esModules + ...originalModule, + getRandom: jest.fn().mockReturnValue(10), + }; +}); + +const getRandom = require('../myModule').getRandom; + +getRandom(); // Always returns 10 +``` + ### `jest.requireMock(moduleName)` Returns a mock module instead of the actual module, bypassing all checks on whether the module should be required normally or not. diff --git a/website/versioned_docs/version-24.0/JestObjectAPI.md b/website/versioned_docs/version-24.0/JestObjectAPI.md index d72a664f9d61..56f81714d73f 100644 --- a/website/versioned_docs/version-24.0/JestObjectAPI.md +++ b/website/versioned_docs/version-24.0/JestObjectAPI.md @@ -322,6 +322,25 @@ _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 +jest.mock('../myModule', () => { + // Require the original module to not be mocked... + const originalModule = jest.requireActual(moduleName); + + return { + __esModule: true, // Use it when dealing with esModules + ...originalModule, + getRandom: jest.fn().mockReturnValue(10), + }; +}); + +const getRandom = require('../myModule').getRandom; + +getRandom(); // Always returns 10 +``` + ### `jest.requireMock(moduleName)` Returns a mock module instead of the actual module, bypassing all checks on whether the module should be required normally or not. From 9c55ef64b4c654faf01a2e2b0feccf27f51e2fec Mon Sep 17 00:00:00 2001 From: sandrina-p Date: Wed, 22 May 2019 13:35:33 +0100 Subject: [PATCH 3/5] Update /jest-environment/src/index.ts - Add example to requireActual --- packages/jest-environment/src/index.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index eb54bade0963..fb122bc8538d 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -137,6 +137,21 @@ export interface Jest { /** * Returns the actual module instead of a mock, bypassing all checks on * whether the module should receive a mock implementation or not. + * + * @example + jest.mock('../myModule', () => { + // Require the original module to not be mocked... + const originalModule = jest.requireActual(moduleName); + return { + __esModule: true, // Use it when dealing with esModules + ...originalModule, + getRandom: jest.fn().mockReturnValue(10), + }; + }); + + const getRandom = require('../myModule').getRandom; + + getRandom(); // Always returns 10 */ requireActual: (moduleName: string) => unknown; /** From 47ee4b5b99492dd87826094ace6974e97440204d Mon Sep 17 00:00:00 2001 From: sandrina-p Date: Wed, 22 May 2019 13:35:46 +0100 Subject: [PATCH 4/5] Update CHANGELOG - docs: Add example to #8482 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3413014fea4..ea66aee969ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Chore & Maintenance - `[jest-leak-detector]` remove code repeat ([#8438](https://github.com/facebook/jest/pull/8438) +- `[docs]` Add example to `jest.requireActual` ([#8482](https://github.com/facebook/jest/pull/8482) ### Performance From d3efbcd828b83dc48e3057d3d8bbe6caf122f767 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 22 May 2019 14:44:28 +0200 Subject: [PATCH 5/5] Update index.ts --- packages/jest-environment/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index fb122bc8538d..3e23757e4ee8 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -139,6 +139,7 @@ export interface Jest { * whether the module should receive a mock implementation or not. * * @example + ``` jest.mock('../myModule', () => { // Require the original module to not be mocked... const originalModule = jest.requireActual(moduleName); @@ -152,6 +153,7 @@ export interface Jest { const getRandom = require('../myModule').getRandom; getRandom(); // Always returns 10 + ``` */ requireActual: (moduleName: string) => unknown; /**