{"record":{"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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/node/automock.ts#L27-L63","documentation":"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.","triggerScenarios":"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.","commonSituations":"Automocking barrel/index files (`export * from './x'`) in Vitest browser mode; migrating Node-mode automock tests to browser mode and hitting the limitation.","solutions":["Provide an explicit factory: `vi.mock(path, factory)` — factories bypass static `export *` analysis.","Refactor the target module to use named re-exports (`export { x } from './other'`) instead of `export *`.","Run the affected tests in Node mode instead of browser mode if automocking the barrel is required."],"exampleFix":"// before — barrel with export *; automock fails in browser\n// barrel.ts: export * from './a'; export * from './b'\nvi.mock('./barrel')\n// after — explicit factory\nvi.mock('./barrel', () => ({ a: vi.fn(), b: vi.fn() }))","handlingStrategy":"validation","validationCode":"// In browser mode, avoid automocking barrels. Detect export * statically before mocking.\nimport { readFileSync } from 'node:fs'\nfunction hasExportStar(source: string): boolean {\n  return /^\\s*export\\s*\\*\\s+from\\s+['\"]/m.test(source)\n}\n// If true, provide a factory instead of bare vi.mock(path).","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Provide explicit factories for barrel/index files in browser mode.","Prefer named re-exports over export * in modules you intend to automock.","Run barrel-heavy tests in Node mode where export * is supported."],"tags":["automock","browser-mode","export-star","vi-mock"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}