From c24934c4478667b03e2707771f19ea3e914ebfca Mon Sep 17 00:00:00 2001 From: Sandrina Pereira Date: Wed, 22 May 2019 14:06:48 +0100 Subject: [PATCH] Add example to jest.requireActual (#8482) --- CHANGELOG.md | 1 + docs/JestObjectAPI.md | 19 +++++++++++++++++++ packages/jest-environment/src/index.ts | 17 +++++++++++++++++ .../version-22.x/JestObjectAPI.md | 19 +++++++++++++++++++ .../version-23.x/JestObjectAPI.md | 19 +++++++++++++++++++ .../version-24.0/JestObjectAPI.md | 19 +++++++++++++++++++ 6 files changed, 94 insertions(+) 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 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. diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index eb54bade0963..3e23757e4ee8 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -137,6 +137,23 @@ 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; /** 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.