-
-
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
feature: jest.isolateModules accept an async function for dynamic import()s #10428
Comments
Especially useful if you are forced to use isolateModules because of this: facebook/react#16012 |
Yeah, this (or something similar) is needed for proper ESM support |
is this something that is still looked into? having async support for isolateModules would be great. Each of my projects has the following patch so that I can use it with The change is pretty straightforward, do you think we can incorporate this into the codebase? I can make a proper PR with test coverage and stuff diff --git a/node_modules/jest-runtime/build/index.js b/node_modules/jest-runtime/build/index.js
index 38c6753..a901ea8 100644
--- a/node_modules/jest-runtime/build/index.js
+++ b/node_modules/jest-runtime/build/index.js
@@ -924,7 +924,7 @@ class Runtime {
}
}
- isolateModules(fn) {
+ async isolateModules(fn) {
if (this._isolatedModuleRegistry || this._isolatedMockRegistry) {
throw new Error(
'isolateModules cannot be nested inside another isolateModules.'
@@ -935,7 +935,7 @@ class Runtime {
this._isolatedMockRegistry = new Map();
try {
- fn();
+ await fn();
} finally {
var _this$_isolatedModule, _this$_isolatedMockRe;
@@ -1741,8 +1741,8 @@ class Runtime {
return jestObject;
};
- const isolateModules = fn => {
- this.isolateModules(fn);
+ const isolateModules = async fn => {
+ await this.isolateModules(fn);
return jestObject;
}; this of course forces you to await |
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. |
🚀 Feature Proposal
Have the ability to pass
jest.isolatedModules
an async function, and have it wait for the promise to resolve before cleaning up the isolated module registry.I think this feature might be kind of tricky, because currently it appears there is at most one isolated module registry in a given runtime, meaning that successive calls to
jest.isolatedModules
with async functions (without waiting for each to resolve) would overlap and fail.Motivation
Right now,
jest.isolateModules()
appears to work for static imports only: https://github.com/facebook/jest/blob/v26.4.0/packages/jest-runtime/src/index.ts#L748. But if you usebabel-jest
and your modules make use of dynamicimport()
s, thefinally
block will clean up the isolated modules registry prior toimport()
s resolving.My specific use-case for this feature is in defining a helper function for integration tests, that sets up my entire application—mocking some key dependencies—and returns a reference to the root page component mounted in the
jsdom
environment. This entrypoint uses multiple dynamic imports during its initialization. I'd like the helper function to be self-contained, as I can't make assumptions about how it's called. It could be called multiple times within a test suite, for example, hence the desire to useisolatedModules
.Example
The text was updated successfully, but these errors were encountered: