vitest never completes when module exports a function named `then`
Describe the bug
Create a file tmp.ts containing the following
export function then() {
return null;
}
in folder __test__ create tmp.test.ts containing the following
import { test } from "vitest";
import { then } from "../tmp";
test("resolve", () => {
then;
});
vitest will never complete running. Renaming the export to e.g. then_ causes vitest to complete as expected.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-seut9k?file=tmp.ts,test%2Ftmp.test.ts&initialPath=vitest
System Info
System:
OS: macOS 12.2.1
CPU: (10) arm64 Apple M1 Pro
Memory: 8.04 GB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.15.1 - /usr/local/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 8.11.0 - /opt/homebrew/bin/npm
Browsers:
Chrome: 103.0.5060.134
Safari: 15.3
npmPackages:
vitest: ^0.18.1 => 0.18.1
Used Package Manager
yarn
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
This bug is because vite-node's directRequest is an async function, and the exports content like
exports = {
[Symbol.toStringTag]: 'Module',
then: function() { ... }
}
so, async will run exports.then then get the pending status and the error exports content.
Hope it is helpful for anyone to fix it.
line https://github.com/vitest-dev/vitest/blob/main/packages/vite-node/src/client.ts#L247
Curious how to solve it~
Doesn't work in Native node either 🤔
https://stackblitz.com/edit/node-2kwr6a?file=index.js,then.mjs
An object with .then is treated as a Thenable, just like a Promise. The code below is ok. 🤔
export const then = (onFulfilled) => {
onFulfilled(null);
};
This is expected behavior. Native Node.js also doesn't finish import.
This might've worked before in Jest because it uses require for module resolution.