vitest icon indicating copy to clipboard operation
vitest copied to clipboard

vitest never completes when module exports a function named `then`

Open philipnilsson opened this issue 3 years ago • 3 comments

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

philipnilsson avatar Aug 01 '22 13:08 philipnilsson

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~

PatrickChen928 avatar Aug 02 '22 10:08 PatrickChen928

Doesn't work in Native node either 🤔

https://stackblitz.com/edit/node-2kwr6a?file=index.js,then.mjs

sheremet-va avatar Aug 05 '22 12:08 sheremet-va

An object with .then is treated as a Thenable, just like a Promise. The code below is ok. 🤔

export const then = (onFulfilled) => {
  onFulfilled(null);
};

nieyuyao avatar Aug 07 '22 09:08 nieyuyao

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.

sheremet-va avatar May 30 '23 18:05 sheremet-va