-
-
Notifications
You must be signed in to change notification settings - Fork 517
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
"TypeError: A dynamic import callback was invoked without --experimental-vm-modules" in Jest #2254
Comments
Hi, @johnal95. Thanks for reporting this! First, it seems to be an issue exclusively with Jest and its still lacking proper support for ESM. Here's why I suspect that.
import { graphql, HttpResponse } from "msw";
import { setupServer } from "msw/node";
const server = setupServer(
graphql.query("GetUser", () => {
return HttpResponse.json({ data: { user: {} } });
})
);
server.listen();
await fetch("https://api.example.com", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `
query GetUser {
user {
id
name
}
}
`,
}),
}); The mention of VM heavily implies Node.js is throwing on how Jest is using the How to fix thisFollow the suggestion from the error: // package.json
{
"scripts": {
- "test": "jest"
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest"
},
} MSW fixWe are not addressing Jest-related issues anymore. As much as I admire Jest, it holds the entire ecosystem back. If you can, use Vitest or other modern testing frameworks. |
@kettanaito I understand the decision. Although it might be worth noting that a considerable amount of test suites out there still run on Jest. And migrating some of these codebases (such as the one where I originally encountered this issue, which has thousands of test files) to e.g. Vitest might not be a small/easy task. I definitely agree with the idea of moving to more modern testing frameworks and towards ESM. But it would be nice to maintain support for Jest for a while longer if possible, to allow for more and/or bigger codebases to adapt. Since the dynamic import of Could it be an option to transpile the dynamic import, or post-process the build output for E.g. try {
// replace `await import` by `require`
- const { parse } = await import('graphql')
+ const { parse } = require('graphql')
// ... rest of implementation
} catch (error) {
devUtils.error('Failed to parse a GraphQL query: cannot import...')
throw error
} (I know it may not look great, but maybe that could keep the lights on a bit longer 🙂) Using the |
Unfortunately, that's only on the surface. It's a symptom of a bigger issue—not installing
This sounds like a good idea. Are you aware of require will remain scoped to that We can have a custom esbuild plugin that will do that for us. Would you help me test that solution in your project? |
@kettanaito out of interest why the decision of modifying the ESM build instead of the CJS? 🤔 I also just tested a very crude example which would produce a very similar outcome in my repro repo, and seems to be working as expected: https://github.com/johnal95/repro-msw-graphql-import-error/tree/master (there is a The idea here being that we could refactor the current code to wrap the require/import in a try catch instead of using Promise catch. Then it's just a matter of replacing What do you think? |
But does the fix I proposed above work?
Mostly due to the I find it much simpler to replace If you can confirm my fix working, I can proceed with publishing it. |
Fair enough.
Just tested and I confirm that does solve the issue. Many thanks! 🙏 |
Released: v2.4.2 🎉This has been released in v2.4.2! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Prerequisites
Environment check
msw
versionNode.js version
20.15.1
Reproduction repository
https://github.com/johnal95/repro-msw-graphql-import-error
Reproduction steps
pnpm test
Current behavior
The
graphql
module fails to be imported, preventing the mocking of GraphQL requests.Likely introduced with 1799e06
Expected behavior
Same bahavior as in the previous version -
2.4.0
.graphql
module is imported successfully and GraphQL requests are mocked as expected.The text was updated successfully, but these errors were encountered: