vitest-dev/vitest · error · TypeError

unknown source type while automocking

Error message

unknown source type while automocking: ${source}

What it means

Thrown as a `TypeError` by `automockModule` when processing an `export *` declaration whose `node.source.value` is not a string. The AST spec requires `ExportAllDeclaration.source.value` to be a string literal; a non-string value indicates a malformed or non-standard AST produced by a buggy parser or a custom parse function.

Solutions

  1. If using a custom parser, ensure it produces a standard ESTree-compatible `ExportAllDeclaration` with a string `source.value`.
  2. File a Vitest bug with the source file and parser configuration if this occurs with the default parser.
Defensive patterns

Strategy: type-guard

Validate before calling

// When using a custom parser, validate the AST shape before automocking.
function hasStringExportStarSource(ast: any): boolean {
  return ast.body.every((node: any) =>
    node.type !== 'ExportAllDeclaration' || typeof node.source?.value === 'string'
  )
}

Type guard

function isExportAllWithstringLiteral(node: unknown): node is { type: 'ExportAllDeclaration'; source: { value: string } } {
  return typeof node === 'object' && node !== null
    && (node as any).type === 'ExportAllDeclaration'
    && typeof (node as any).source?.value === 'string'
}

Prevention

When it happens

Trigger: A custom `parse` function passed to `automockModule` returns an AST where `ExportAllDeclaration.source.value` is a node/object instead of a string; or a corrupted/instrumented source produces an unexpected AST shape. Under stock Vitest with the default acorn parser this should be unreachable.

Common situations: Forking Vitest or injecting a custom parser into the automock pipeline; processing source that has been pre-transformed into a non-standard AST representation.

Related errors


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

Appendix: source

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

  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) => {
          const isReexported = allSpecifiers.some(({ name, alias }) => name === exportName || alias === exportName)
          if (!isReexported) {
            importNames.push(exportName)
            allSpecifiers.push({ name: exportName })
          }
        })

View on GitHub (pinned to 1fa9837ec2)