question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Disable all manual `__mocks__` at once with `jest.unmockAll()`.

See original GitHub issue

šŸš€ Feature Proposal

Disable all manual __mocks__ at once with jest.unmockAll(), including the manual mocks for node_modules.

Motivation

Historically, the main pattern for testing UI was to focus on unit tests that tested UI components in isolation. With this pattern, it is common to mock modules so the internal details of nested components would not leak into the tests of the component under test. One canonical example was to provide manual mocks for common shared component (via __mocks__) so the dom elements of the nested components would not show up in snapshots.

With the advent of new testing libraries such as testing-library, there has been a trend to write tests that closely resemble how components are used. In practice, that usually means mocking as little as possible. In this context, we generally do not want third-party node_modules to be mocked and we have to call unmock for all the manual node modules __mocks__ that the component under test indirectly relies on.

This is very cumbersome for a couple of reasons:

  1. There might be many of those manual __mocks__ in codebases that either want to support both testing patterns at the same time, or that are slowly migrating toward one or the other pattern.
  2. There is no way to know what __mocks__ modules were used by jest during a test. You currently need to check manually the code of every single indirect dependency of a module.

This is completely impractical for large codebases.

The same way we can disable all auto-mocked modules with jest.disableAutomock(), there should be a way to disable all manually mocked modules with jest.unmockAll(). Ideally, we would also expose a manualMock boolean parameter in jest config so it can be disabled across multiple tests at once.

Example

jest.unmockAll()

Pitch

Since manual __mocks__ are a jest concept, the only way to change is to update jest itself.

Iā€™m happy to send a PR if the maintainers of jest are fine with this feature. Let me know and Iā€™ll work on it asap.

Cheers

EDIT: Made it clearer that the problem is for __mocks__ of node_modules

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:28
  • Comments:27

github_iconTop GitHub Comments

16reactions
jnakcommented, Aug 16, 2019

Yeah I potentially might mock some modules afterwards. Essentially, I just want to tell jest donā€™t mock anything unless I told you to.

15reactions
merowarecommented, Jul 18, 2020

I also agree that we need a way to disable manual mocks in case we have different types of tests. e.g integration or e2e. In the mean time I found a work around for this that avoids having to call jest.unmock everywhere.

I make jest ignore path patterns for __mocks__ made for special cases in my code when I donā€™t use automock, as well as ignore the root __mocks__ meant to manual mock node_modules on special cases where automocks couldnā€™t do the job.

modulePathIgnorePatterns: ['<rootDir>/src/.*/__mocks__', '<rootDir>/__mocks__'],

If by any reason, you still need to mock certain libs in your integration tests that are used in every test file then you can pass jest a node script in setupFilesAfterEnv that calls your jest.mock on whatever module you need and provide it the manual mock there. Or as stated above, call jest.mock on each test file separately. Itā€™s a bit of a wrapper while we get this feature figured out. e.g jest.mock('<module name>', () => require('./__mocks__/<module name>'));

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I disable a jest mock for a particular test?
My context for wanting to turn off a mock was a manual mock of a node_module dependency. Using jest.unmock('module-name') worked for me. ā€“Ā ......
Read more >
Manual Mocks - Jest
Manual mocks are used to stub out functionality with mock data. ... a module that provides a summary of all the files in...
Read more >
Jest Object (API Reference) - w3resource
s overall behavior. Mock Modules. jest.disableAutomock(). This will disable mocking in the module loader. Once this method is called, all ...
Read more >
The Jest Object
Disables automatic mocking in the module loader. After this method is called, all require() s will return the real versions of each module...
Read more >
How to automatically reset mocks and restore spies in Jest
When using Jest it seemed to be a common approach to manually invoke jest. ... ā€œclearMocksā€: true : resets all the mocks usage...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found