-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Error on dynamic imports using AsyncConstructor
in Vitest
#960
Comments
eval
or new Function()
in VitestAsyncConstructor
in Vitest
Native imports are not possible, until Node releases full ESM support for vm module. Should we just statically replace it? @antfu |
Sadly I think it might be the limitations currently. I would like to avoid adding more magic to transform it until we got better support from Node. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Any updates or solutions about this issue? Face the same error while using dynamic import in vitest. |
I faced a similar problem, which prevents me from developing my programming language whose current compile target is JavaScript. |
Vite transforms all |
This feature should work correctly with |
Hi @sheremet-va, I tried both TypeError: A dynamic import callback was not specified.
at new NodeError (node:internal/errors:405:5)
at hydrateFn (node:internal/modules/esm/utils:116:9)
at eval (eval at runAsync (/Users/talatkuyuk/MyCodeRepo/my_packages/next-mdx-remote-client/src/lib/run.ts:68:29), <anonymous>:7:29)
at Module.runAsync (/Users/talatkuyuk/MyCodeRepo/my_packages/next-mdx-remote-client/src/lib/run.ts:69:46)
at Module.evaluate (/Users/talatkuyuk/MyCodeRepo/my_packages/next-mdx-remote-client/src/rsc/evaluate.tsx:54:15)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at /Users/talatkuyuk/MyCodeRepo/my_packages/next-mdx-remote-client/tests/test.evaluate.esm.spec.tsx:77:32
at runTest (file:///Users/talatkuyuk/MyCodeRepo/my_packages/next-mdx-remote-client/node_modules/@vitest/runner/dist/index.js:719:11)
at runSuite (file:///Users/talatkuyuk/MyCodeRepo/my_packages/next-mdx-remote-client/node_modules/@vitest/runner/dist/index.js:847:15)
at runSuite (file:///Users/talatkuyuk/MyCodeRepo/my_packages/next-mdx-remote-client/node_modules/@vitest/runner/dist/index.js:847:15) {
code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING'
} the mentioned export async function runAsync(
compiledSource: string,
options: RunOptions,
): Promise<RunResult> {
const { keys, values } = prepareConstruction(options);
const AsyncFunction = async function () {}.constructor;
// await new Promise((resolve) => setTimeout(resolve, 500));
// constructs the compiled source utilizing Reflect API with "async function constructor"
const hydrateFn = Reflect.construct(AsyncFunction, keys.concat(compiledSource));
const { default: Content, ...mod } = await hydrateFn(...values);
return { Content, mod };
} My nextjs application works perfect, but couldn't pass the test because of that error. I was using I converted the test using my SORRY, SORRY, SORRY But, Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: Worker exited unexpectedly
❯ ChildProcess.<anonymous> node_modules/tinypool/dist/esm/index.js:184:34
❯ ChildProcess.emit node:events:529:35
❯ ChildProcess._handle.onexit node:internal/child_process:292:12 My test script is: test.only("works with imported modules", async () => {
const source = dedent`
import {Pill} from "./context/components.js"
Hi {name}
<Pill>!</Pill>
`;
const { content } = await evaluate({
source,
options: {
mdxOptions: {
baseUrl: import.meta.url, // should be provided, finds the 'components.js' in relative path
},
scope: {
name: "foo",
},
},
});
expect(ReactDOMServer.renderToStaticMarkup(content)).toMatchInlineSnapshot(`
"<p>Hi foo</p>
<span style="color:blue">!</span>"
`);
}); |
Describe the bug
When using dynamic imports with
AsyncConstructor
:It works fine in Node:
But, when run with Vitest, Vitest throws the following error:
Might be related to this error: nodejs/node#30591
Reproduction
https://github.com/leonzalion/vitest-dynamic-import-eval
System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: