Skip to content

Commit 2b13547

Browse files
authored
fix: apply cjs interop for truthy __esModule (#10363)
1 parent d3c964b commit 2b13547

5 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎packages/vitest/src/runtime/moduleRunner/moduleEvaluator.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ function interopModule(mod: any) {
531531

532532
let defaultExport = 'default' in mod ? mod.default : mod
533533

534-
if (!isPrimitive(defaultExport) && '__esModule' in defaultExport) {
534+
if (!isPrimitive(defaultExport) && defaultExport.__esModule) {
535535
mod = defaultExport
536536
if ('default' in defaultExport) {
537537
defaultExport = defaultExport.default
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.__esModule = false
2+
exports.test = 'hello'

‎test/unit/deps/dep-cjs/esmodule.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.__esModule = true
2+
exports.test = 'hello'

‎test/unit/deps/dep-cjs/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"name": "@vitest/test-dep-cjs",
33
"type": "commonjs",
44
"exports": {
5+
"./esmodule": "./esmodule.js",
6+
"./esmodule-false": "./esmodule-false.js",
57
"./esm-comment": "./esm-comment.js",
68
"./esm-string": "./esm-string.js"
79
}

‎test/unit/test/interop.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// @ts-expect-error no type
2+
import * as esModule from '@vitest/test-dep-cjs/esmodule'
3+
// @ts-expect-error no type
4+
import * as esModuleFalse from '@vitest/test-dep-cjs/esmodule-false'
5+
import { expect, test } from 'vitest'
6+
7+
const nodeMajor = Number(process.versions.node.split('.')[0])
8+
9+
test('interop', async ({ task }) => {
10+
expect(esModule).toMatchInlineSnapshot(`
11+
{
12+
"__esModule": true,
13+
"test": "hello",
14+
}
15+
`)
16+
if (task.file.projectName === 'vmThreads' || nodeMajor < 23) {
17+
expect(esModuleFalse).toMatchInlineSnapshot(`
18+
{
19+
"__esModule": false,
20+
"default": {
21+
"__esModule": false,
22+
"test": "hello",
23+
},
24+
"test": "hello",
25+
}
26+
`)
27+
}
28+
else {
29+
expect(esModuleFalse).toMatchInlineSnapshot(`
30+
{
31+
"__esModule": false,
32+
"default": {
33+
"__esModule": false,
34+
"test": "hello",
35+
},
36+
"module.exports": {
37+
"__esModule": false,
38+
"test": "hello",
39+
},
40+
"test": "hello",
41+
}
42+
`)
43+
}
44+
})

0 commit comments

Comments
 (0)