-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Labels
Description
Describe the bug
Vitest 4's Module Runner doesn't apply Vite plugins when resolving fully dynamic imports (imports with variable paths). This causes virtual modules and other plugin-resolved modules to fail. This does not apply to local paths – just node modules.
I isolated this issue when debugging problems with Vitest 4 and Astro, because it means imports from astro do not resolve Astro's virtual modules. It is a regression, and works in Vitest 3.
There is a minimal reproduction in Stackblitz which passes in 3 and fails in 4.
import { test, expect } from 'vitest';
const entrypoint = 'my-module';
const mod = await import(entrypoint);
test('loads module', () => {
expect(mod.default.value).toEqual(999);
});...where my-module imports from a virtual module defined in a Vite plugin.
import config from 'fake:config';
export default config/// <reference types="vitest" />
import { defineConfig } from 'vitest/config';
const fakeVirtualModule = {
name: 'fake-virtual-module',
resolveId(id: string) {
if (id === 'fake:config') {
return 'fake:config';
}
},
load(id: string) {
if (id === 'fake:config') {
return 'export default { value: 999 };';
}
},
};
export default defineConfig({
plugins: [fakeVirtualModule],
test: {},
});Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-y82kvuo5
System Info
System:
OS: macOS 15.0
CPU: (8) arm64 Apple M1
Memory: 147.66 MB / 8.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.17.0 - /Users/matt/.nvm/versions/node/v22.17.0/bin/node
npm: 10.9.2 - /Users/matt/.nvm/versions/node/v22.17.0/bin/npm
pnpm: 9.15.6 - /Users/matt/.nvm/versions/node/v22.17.0/bin/pnpm
npmPackages:
vitest: ^4.0.8 => 4.0.8Used Package Manager
pnpm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
ematipico