vitest-dev/vitest · error · TypeError

unknown source type while automocking: ${source}

Error message

unknown source type while automocking: ${source}

What it means

Thrown at packages/mocker/src/node/automock.ts:50-53 as a TypeError when an ExportAllDeclaration's source.value is not a string. AST source values are normally string literals, so a non-string source indicates a malformed or unexpected AST shape produced by the parser, which the automocker cannot resolve to a module path.

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 d568f8ce37)

Solutions

  1. Provide an explicit vi.mock factory to bypass automock parsing of the offending module.
  2. Avoid non-standard `export *` forms in the mocked module.
  3. Report with a reproduction including the source file, as this indicates an unhandled AST shape.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  vi.mock('./oddModule.ts')
} catch (e) {
  if (e instanceof Error && /unknown source type while automocking/.test(e.message)) {
    vi.mock('./oddModule.ts', () => ({}))
  } else { throw e }
}

Prevention

When it happens

Trigger: The parser yields an ExportAllDeclaration whose source.value is not a string (e.g. a non-literal source, or a parser quirk). Then `unknown source type while automocking: ${source}` reports the actual value.

Common situations: Parser divergence on unusual `export *` constructs; a custom parse callback returning a non-standard AST; edge-case syntax the parser represents differently than expected.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/aa0c428579dd3bc9.json. Report an issue: GitHub.