{"record":{"id":"7efbb2da39bdfb6f","repo":"vitest-dev/vitest","slug":"failed-to-parse-options-id","errorCode":null,"errorMessage":"failed to parse ${options.id}","messagePattern":"failed to parse (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/node/automock.ts","lineNumber":30,"sourceCode":"  globalThisAccessor?: string\n  id?: string\n}\n\n// TODO: better source map replacement\nexport function automockModule(\n  code: string,\n  mockType: 'automock' | 'autospy',\n  parse: (code: string) => any,\n  options: AutomockOptions = {},\n): MagicString {\n  const globalThisAccessor = options.globalThisAccessor || '\"__vitest_mocker__\"'\n  let ast: Program\n  try {\n    ast = parse(code) as Program\n  }\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      }","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/node/automock.ts#L12-L48","documentation":"Thrown by `automockModule` when the provided `parse` function fails to parse the module source and `options.id` is set. The original parse error is attached as `cause`. Automocking transforms a module's source into a mocked version, so it must parse the AST first; a parse failure means the source contains syntax the configured parser (typically acorn) rejects.","triggerScenarios":"`vi.mock(path)` (without a factory) is called against a module whose source cannot be parsed by the automock pipeline — e.g., it uses newer ECMAScript syntax than the parser supports, contains a genuine syntax error, or uses TypeScript decorators/JSX that the plain acorn parser does not understand (note: TS type-stripping is handled separately by `transformCode`).","commonSituations":"Automocking a module that uses stage-3/4 syntax not yet supported by the bundled acorn; a source file with an actual syntax error that Vite tolerates via a different parser; automocking a `.tsx`/`.jsx` file whose JSX is not pre-stripped before reaching the automock parser.","solutions":["Inspect the `cause` on the thrown error for the underlying parse error and line number.","Fix the syntax error in the target module if one exists.","Provide an explicit factory to `vi.mock(path, factory)` to bypass automock parsing entirely.","If the syntax is valid modern JS, update Vitest — newer versions bundle newer parsers."],"exampleFix":"// before — automock fails to parse the module\nvi.mock('./broken-mod')\n// after — provide a factory to skip parsing\nvi.mock('./broken-mod', () => ({ fn: vi.fn() }))","handlingStrategy":"fallback","validationCode":"// Detect unparseable source before automock by attempting a parse in a scratch build step, or simply prefer explicit factories for modules known to use unsupported syntax.","typeGuard":null,"tryCatchPattern":"try {\n  vi.mock('./mod') // automock path\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('failed to parse')) {\n    vi.mock('./mod', () => ({ /* explicit stubs */ }))\n  }\n}","preventionTips":["Provide an explicit factory for modules using leading-edge or non-standard syntax.","Keep Vitest updated so the bundled parser supports newer ECMAScript features.","Read err.cause for the exact parse failure and line number before changing code."],"tags":["automock","parser","ast","vi-mock"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}