vitest-dev/vitest · error · Error

automocking files with `export *` is not supported because…

Error message

automocking files with `export *` is not supported because it cannot be easily statically analysed

What it means

Thrown by `automockModule` when the module being automocked contains an `export *` (re-export-all) declaration and `options.id` is not set — which is the browser-mode path. Resolving `export *` requires reading the re-exported module from disk to enumerate its exports statically, which is not possible without the filesystem context that `options.id` (the absolute file path) provides.

Solutions

  1. Provide an explicit factory: `vi.mock(path, factory)` — factories bypass static `export *` analysis.
  2. Refactor the target module to use named re-exports (`export { x } from './other'`) instead of `export *`.
  3. Run the affected tests in Node mode instead of browser mode if automocking the barrel is required.

Example fix

// before — barrel with export *; automock fails in browser
// barrel.ts: export * from './a'; export * from './b'
vi.mock('./barrel')
// after — explicit factory
vi.mock('./barrel', () => ({ a: vi.fn(), b: vi.fn() }))
Defensive patterns

Strategy: validation

Validate before calling

// In browser mode, avoid automocking barrels. Detect export * statically before mocking.
import { readFileSync } from 'node:fs'
function hasExportStar(source: string): boolean {
  return /^\s*export\s*\*\s+from\s+['"]/m.test(source)
}
// If true, provide a factory instead of bare vi.mock(path).

Prevention

When it happens

Trigger: In browser mode, calling `vi.mock(path)` (automock, no factory) on a module that contains `export * from './other'`. The browser-side automock cannot resolve and read `./other` from the filesystem, so it refuses rather than producing an incomplete mock.

Common situations: Automocking barrel/index files (`export * from './x'`) in Vitest browser mode; migrating Node-mode automock tests to browser mode and hitting the limitation.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/10f80f83c6200f03. Report an issue: GitHub.

Appendix: source

Thrown at packages/mocker/src/node/automock.ts:45

  }
  catch (cause) {
    if (options.id) {
      throw new Error(`failed to parse ${options.id}`, { cause })
    }
    throw cause
  }

  const m = new MagicString(code)

  const allSpecifiers: { name: string; alias?: string }[] = []
  const replacers: (() => void)[] = []
  let importIndex = 0
  for (const _node of ast.body) {
    if (_node.type === 'ExportAllDeclaration') {
      const node = _node as Positioned<ExportAllDeclaration>
      // TODO: pass it down in the browser mode
      if (!options.id) {
        throw new Error(
          `automocking files with \`export *\` is not supported because it cannot be easily statically analysed`,
        )
      }

      const source = node.source.value
      if (typeof source !== 'string') {
        throw new TypeError(`unknown source type while automocking: ${source}`)
      }

      const moduleUrl = import.meta.resolve(source, pathToFileURL(options.id).toString())
      const modulePath = fileURLToPath(moduleUrl)
      const moduleContent = readFileSync(modulePath, 'utf-8')
      const transformedCode = transformCode(moduleContent, moduleUrl)
      const moduleFormat = resolveModuleFormat(moduleUrl, transformedCode)
      const moduleExports = collectModuleExports(modulePath, transformedCode, moduleFormat || 'module')
      replacers.push(() => {
        const importNames: string[] = []
        moduleExports.forEach((exportName) => {

View on GitHub (pinned to 1fa9837ec2)