From 0e7f2feceade4edbee31783728255561adbf13cb Mon Sep 17 00:00:00 2001 From: Sandrina Pereira Date: Wed, 22 May 2019 12:34:33 +0100 Subject: [PATCH] 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.