{"id":"10f80f83c6200f03","repo":"vitest-dev/vitest","slug":"automocking-files-with-export-is-not-supported","errorCode":null,"errorMessage":"automocking files with `export *` is not supported because it cannot be easily statically analysed","messagePattern":"automocking files with `export \\*` is not supported because it cannot be easily statically analysed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/node/automock.ts","lineNumber":45,"sourceCode":"  }\n  catch (cause) {\n    if (options.id) {\n      throw new Error(`failed to parse ${options.id}`, { cause })\n    }\n    throw cause\n  }\n\n  const m = new MagicString(code)\n\n  const allSpecifiers: { name: string; alias?: string }[] = []\n  const replacers: (() => void)[] = []\n  let importIndex = 0\n  for (const _node of ast.body) {\n    if (_node.type === 'ExportAllDeclaration') {\n      const node = _node as Positioned<ExportAllDeclaration>\n      // TODO: pass it down in the browser mode\n      if (!options.id) {\n        throw new Error(\n          `automocking files with \\`export *\\` is not supported because it cannot be easily statically analysed`,\n        )\n      }\n\n      const source = node.source.value\n      if (typeof source !== 'string') {\n        throw new TypeError(`unknown source type while automocking: ${source}`)\n      }\n\n      const moduleUrl = import.meta.resolve(source, pathToFileURL(options.id).toString())\n      const modulePath = fileURLToPath(moduleUrl)\n      const moduleContent = readFileSync(modulePath, 'utf-8')\n      const transformedCode = transformCode(moduleContent, moduleUrl)\n      const moduleFormat = resolveModuleFormat(moduleUrl, transformedCode)\n      const moduleExports = collectModuleExports(modulePath, transformedCode, moduleFormat || 'module')\n      replacers.push(() => {\n        const importNames: string[] = []\n        moduleExports.forEach((exportName) => {","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/node/automock.ts#L27-L63","documentation":"Thrown at packages/mocker/src/node/automock.ts:41-48 when automockModule encounters an ExportAllDeclaration (export * from '...') and options.id is not set. Resolving the re-exported module's exports requires reading the source from disk via options.id (import.meta.resolve at automock.ts:55), so without an id the re-export cannot be statically analyzed and automocking aborts.","triggerScenarios":"Automocking a module that contains `export * from './other'` when automockModule is called without options.id. The browser path historically omits id, hence the TODO comment about browser mode.","commonSituations":"Automocking a barrel/index file that re-exports everything; automocking in browser mode where id is not forwarded; mocking a library entry that uses export *.","solutions":["Provide an explicit factory so automock parsing is skipped: vi.mock('./barrel', () => ({ ... })).","Mock the individual modules the barrel re-exports instead of the barrel itself.","Ensure options.id is passed when invoking automockModule directly (node path passes id; browser path may not)."],"exampleFix":"// before\nvi.mock('./index') // index has `export * from './users'`\n\n// after\nvi.mock('./index', () => ({\n  getUser: vi.fn(),\n}))","handlingStrategy":"validation","validationCode":"// Detect export * in the target before automocking and fall back to a factory.\nimport { readFileSync } from 'node:fs'\nfunction hasExportStar(file: string): boolean {\n  return /export\\s+\\*\\s+from/.test(readFileSync(file, 'utf-8'))\n}\n\nif (hasExportStar('./barrel.ts')) {\n  vi.mock('./barrel.ts', () => ({ /* explicit exports */ }))\n} else {\n  vi.mock('./barrel.ts')\n}","typeGuard":null,"tryCatchPattern":"try {\n  vi.mock('./barrel.ts')\n} catch (e) {\n  if (e instanceof Error && /export \\*/.test(e.message)) {\n    vi.mock('./barrel.ts', () => ({ /* explicit exports */ }))\n  } else { throw e }\n}","preventionTips":["Avoid automocking barrel/index files that use export *; provide a factory instead.","Mock individual re-exported modules rather than the barrel.","For browser automocking, prefer explicit factories since id is not forwarded."],"tags":["automock","export-star","barrel","static-analysis"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}