Scoped module initialization / cleanup
See original GitHub issue🚀 Feature Proposal
Like jest.resetModules()
, but only forgets whatever was initialized inside an explicit scope.
jest.withResetModules(() => {
// Jest will snapshot the module registry when we enter,
// and reset any newly loaded (!) modules when we exit.
});
Open to better naming.
Motivation
We often use jest.resetModules()
in the React codebase to isolate individual tests.
However, sometimes we also use jest.resetModules()
for another purpose. We have some code that’s shared between different packages, but ends up being bundled (i.e. copied) into all of them after the build time. But we run Jest on the source (building would’ve been too slow).
As a result, with our setup there might be some accidentally shared state when running the tests that doesn’t end up being shared in practice after the build. This can lead to weird bugs where tests tell us everything is okay, but it breaks in production. We’ve reduced this somewhat by running a subset of tests on compiled bundles, but this is still complicating some further work we need to do.
Usually we work around it like this:
React = require('react');
ReactDOM = require('react-dom');
jest.resetModules();
ReactDOMServer = require('react-dom/server');
This ensures ReactDOM
and ReactDOMServer
are properly isolated even if they happen to share some internal modules. The problem is that this wipes out all the modules, including React
. In some cases it matters for us that React
stays the same, but modules that were loaded when initializing ReactDOM
or ReactDOMServer
are discarded.
Example
var React;
var ReactDOM;
var ReactTestUtils;
var ReactDOMServer;
// React (and whatever it loaded) should be reused
React = require('react');
// Load some modules and then clear them from the cache
jest.withResetModules(() => {
ReactDOM = require('react-dom');
ReactTestUtils = require('react-dom/test-utils');
// When we exit the scope, not only react-dom and react-dom/test-utils
// are cleared from the cache, but also any their transitive dependencies
// that have been loaded for the first time inside the current scope.
});
// Load other modules and then clear them from the cache
jest.withResetModules(() => {
ReactDOMServer = require('react-dom/server');
});
Pitch
I tried my best above. Maybe if there was a way to clear individual modules that would work for me although I need to clear all recursive dependencies that haven’t already been loaded earlier.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:16 (8 by maintainers)
Top GitHub Comments
That it has to happen within a single test case (
it
/test
). To achieve reuse the user can extract it into a function, but I don’t think it’s possible to make this persist between/across testsThis issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.