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:
- 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. - There is no way to know what
__mocks__
modules were used byjest
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:
- Created 4 years ago
- Reactions:28
- Comments:27
Top GitHub Comments
Yeah I potentially might mock some modules afterwards. Essentially, I just want to tell
jest
donāt mock anything unless I told you to.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.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.gjest.mock('<module name>', () => require('./__mocks__/<module name>'));