vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.87k stars 1.15k forks source link

Export named 'then' gets called when module is loaded #6685

Closed robintown closed 4 hours ago

robintown commented 4 hours ago

Describe the bug

If one of the modules in your project has an exported function named then, Vitest will call this function when loading the module. This can either cause your tests to fail or cause Vitest to hang, depending on whether the function returns or throws.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-qfykda?file=src%2Fbasic.ts

System Info

System:
    OS: Linux 6.6 NixOS 24.11 (Vicuna) 24.11 (Vicuna)
    CPU: (8) x64 Intel(R) Core(TM) i7-8665U CPU @ 1.90GHz
    Memory: 10.07 GB / 15.26 GB
    Container: Yes
    Shell: 3.7.1 - /run/current-system/sw/bin/fish
  Binaries:
    Node: 22.2.0 - /nix/store/72m3szv59j74b12dmicmayvvlikh65qc-nodejs-22.2.0/bin/node
    npm: 10.7.0 - /nix/store/72m3szv59j74b12dmicmayvvlikh65qc-nodejs-22.2.0/bin/npm
    pnpm: 9.4.0 - /nix/store/xrhk9dzazk4z5s7ba5n136lq25cwf7wz-corepack-nodejs-22.2.0/bin/pnpm
  Browsers:
    Chromium: 129.0.6668.89
  npmPackages:
    @vitejs/plugin-react: ^4.0.4 => 4.2.1 
    vite: ^4.4.9 => 4.5.3 
    vitest: ^2.0.3 => 2.0.3

Used Package Manager

pnpm

Validations

hi-ogawa commented 4 hours ago

This is a known issue of Vite SSR as we replace static import with dynamic import and needs await on promise. Duplicate of https://github.com/vitest-dev/vitest/issues/5122

Node doesn't support such dynamic import either https://stackblitz.com/edit/vitest-dev-vitest-xmakvp?file=repro.js, so I would imagine then export needs to be avoided in general.