-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Requiring modules with isolateModules does not work if module has already been required with a non-isolated method e.g: using require. #7863
Comments
@rogeliog mind taking a look at this? |
I also ran into this. My use case is: const thing = require('../thing');
describe('thing', () => {
// many tests involving thing
// ...
test('some special case', () => {
jest.isolateModules(() => {
jest.mock('some-module-that-thing-interacts-with', ...);
thing = require('../thing');
// some assertion involving thing
});
});
}); |
I think this should at least be documented since the docs actually show a second import outside the isolatedModules: let myModule;
jest.isolateModules(() => {
myModule = require('myModule');
});
const otherCopyOfMyModule = require('myModule'); |
Any progress on this issue? |
I'm working on patch for this issue, and the behaviour seems to be originated here, where if (
this._moduleRegistry.get(modulePath) ||
!this._isolatedModuleRegistry
) {
moduleRegistry = this._moduleRegistry;
} else {
moduleRegistry = this._isolatedModuleRegistry;
}
} The if condition seems quite a bit too explicit to look like a bug. I'm wandering whether this was the intended behaviour or not. |
This 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. |
🐛 Bug Report
Requiring modules with isolateModules does not work if module has already been required with a non-isolated method e.g: using require.
To Reproduce
Expected behavior
Requiring a module should not have an affect to an isolated module registry.
Run
npx envinfo --preset jest
The text was updated successfully, but these errors were encountered: