Skip to content

Commit 7e6c37a

Browse files
authored
fix: do not throw when importing a type from an external package (#8875)
1 parent 3e19f27 commit 7e6c37a

File tree

8 files changed

+28
-0
lines changed

8 files changed

+28
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ export class VitestModuleRunner extends viteModuleRunner.ModuleRunner {
8787
}
8888
}
8989

90+
/**
91+
* Vite checks that the module has exports emulating the Node.js behaviour,
92+
* but Vitest is more relaxed.
93+
*
94+
* We should keep the Vite behavour when there is a `strict` flag.
95+
* @internal
96+
*/
97+
processImport(exports: Record<string, any>): Record<string, any> {
98+
return exports
99+
}
100+
90101
public async import(rawId: string): Promise<any> {
91102
const resolved = await this.vitestOptions.transport.resolveId(rawId)
92103
return super.import(resolved ? resolved.url : rawId)

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/core/deps/dep-esm-non-existing/index.mjs

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type NonExported = true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@test/dep-esm-non-existing",
3+
"type": "module",
4+
"main": "./index.mjs"
5+
}

test/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"devDependencies": {
1616
"@standard-schema/spec": "^1.0.0",
17+
"@test/dep-esm-non-existing": "link:./deps/dep-esm-non-existing",
1718
"@test/vite-environment-external": "link:./projects/vite-environment-external",
1819
"@test/vite-external": "link:./projects/vite-external",
1920
"@types/debug": "catalog:",

test/core/test/imports.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { mkdir, writeFile } from 'node:fs/promises'
22
import { fileURLToPath } from 'node:url'
3+
import { NonExported } from '@test/dep-esm-non-existing'
34
import { resolve } from 'pathe'
45
import { describe, expect, test, vi } from 'vitest'
56
// @ts-expect-error module is not typed
67
import promiseExport from '../src/cjs/promise-export'
78

89
import { dynamicRelativeImport } from '../src/relative-import'
910

11+
test('can import type from an ESM dependency', () => {
12+
// @ts-expect-error NonExported is type
13+
expect(NonExported).toBe(undefined)
14+
})
15+
1016
test('promise export works correctly', async () => {
1117
await expect(promiseExport).resolves.toEqual({ value: 42 })
1218
})

test/core/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export default defineConfig({
9191
/packages\/web-worker/,
9292
/\.wasm$/,
9393
/\/wasm-bindgen-no-cyclic\/index_bg.js/,
94+
/dep-esm-non-existing/,
9495
],
9596
inline: ['inline-lib'],
9697
},

0 commit comments

Comments
 (0)