vitest-dev / vitest

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

Import discrepancy versus node (and tsx) #6640

Open wparsons-heins-globality opened 2 days ago

wparsons-heins-globality commented 2 days ago

Describe the bug

When importing a CJS module I'm finding that I'm getting different results in vitest when compared with node and tsx.

For instance:

import pkg from 'some-cjs-package';

// node / tsx
// pkg = { default: { defaultExportedFunction } }

// vitest
// pkg = { defaultExportedFunction }

Here's a minimized version of the CJS file I'm importing:

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var specs = {};
exports.default = specs;

Reproduction

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

System Info

System:
    OS: macOS 14.5
    CPU: (11) arm64 Apple M3 Pro
    Memory: 1.25 GB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.16.0 - ~/.local/state/fnm_multishells/36412_1728045710155/bin/node
    Yarn: 1.22.22 - ~/.local/state/fnm_multishells/36412_1728045710155/bin/yarn
    npm: 10.8.1 - ~/.local/state/fnm_multishells/36412_1728045710155/bin/npm
  Browsers:
    Brave Browser: 129.1.70.123
    Chrome: 129.0.6668.90
    Safari: 17.5
  npmPackages:
    vitest: ^2.0.5 => 2.0.5

Used Package Manager

yarn

Validations

sheremet-va commented 2 days ago

This is by design. You can disable this with deps.interopDefault option.