Skip to content

v4: plugins not applied to fully dynamic imports of node modules #8980

@ascorbic

Description

@ascorbic

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.8

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions