Skip to content

Commit

Permalink
Update JestObjectAPI.md - Add example to jest.requireActual
Browse files Browse the repository at this point in the history
I've spent  1 hour looking how to proper use it until I found this comment: #936 (comment)

Too good to not be in the docs.
  • Loading branch information
sandrina-p authored and Sandrina Pereira committed May 22, 2019
1 parent 74f6faf commit cf221ce
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/JestObjectAPI.md
Expand Up @@ -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.
Expand Down

0 comments on commit cf221ce

Please sign in to comment.