vitest-dev/vitest · error · TypeError

"snapshotFormat.compareKeys" function is not supported.

Error message

"snapshotFormat.compareKeys" function is not supported.

What it means

Snapshot formatting config is serialized across worker boundaries, so a function passed as `snapshotFormat.compareKeys` cannot survive the trip. The check at resolveConfig.ts:619 throws a TypeError when `compareKeys` is a function, because the snapshot library would silently lose the key-comparison logic in workers.

Source

Thrown at packages/vitest/src/node/config/resolveConfig.ts:620

  if (resolved.snapshotEnvironment) {
    resolved.snapshotEnvironment = resolvePath(
      resolved.snapshotEnvironment,
      resolved.root,
    )
  }

  resolved.testNamePattern = resolved.testNamePattern
    ? resolved.testNamePattern instanceof RegExp
      ? resolved.testNamePattern
      : new RegExp(resolved.testNamePattern)
    : undefined

  if (resolved.snapshotFormat && 'plugins' in resolved.snapshotFormat) {
    (resolved.snapshotFormat as any).plugins = []
    // TODO: support it via separate config (like DiffOptions) or via `Function.toString()`
    if (typeof resolved.snapshotFormat.compareKeys === 'function') {
      throw new TypeError(`"snapshotFormat.compareKeys" function is not supported.`)
    }
  }

  const UPDATE_SNAPSHOT = resolved.update || process.env.UPDATE_SNAPSHOT
  resolved.snapshotOptions = {
    expand: resolved.expandSnapshotDiff ?? false,
    snapshotFormat: resolved.snapshotFormat || {},
    updateSnapshot:
      UPDATE_SNAPSHOT === 'all' || UPDATE_SNAPSHOT === 'new' || UPDATE_SNAPSHOT === 'none'
        ? UPDATE_SNAPSHOT
        : isCI && !UPDATE_SNAPSHOT ? 'none' : UPDATE_SNAPSHOT ? 'all' : 'new',
    resolveSnapshotPath: options.resolveSnapshotPath,
    // resolved inside the worker
    snapshotEnvironment: null as any,
  }

  resolved.snapshotSerializers ??= []
  resolved.snapshotSerializers = resolved.snapshotSerializers.map(file =>

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Remove the function form of compareKeys; rely on the default string comparison.
  2. If you need custom key ordering, sort object keys before snapshotting rather than via compareKeys.
  3. Track the TODO in the source (line 618): a serializable form may be added later; for now functions are unsupported.

Example fix

// before
export default defineConfig({ test: { snapshotFormat: { compareKeys: (a, b) => a.toLowerCase().localeCompare(b.toLowerCase()) } } })
// after
export default defineConfig({ test: { snapshotFormat: {} } })
Defensive patterns

Strategy: type-guard

Validate before calling

function assertNoFunctionCompareKeys(snapshotFormat: any) {
  if (snapshotFormat && typeof snapshotFormat.compareKeys === 'function') {
    throw new Error('snapshotFormat.compareKeys cannot be a function in config.')
  }
}

Type guard

function isSnapshotFormatSerializable(snapshotFormat: any): boolean {
  return !(snapshotFormat && typeof snapshotFormat.compareKeys === 'function')
}

Prevention

When it happens

Trigger: Set `snapshotFormat: { compareKeys: (a, b) => a.localeCompare(b) }` in vitest.config.ts.

Common situations: Wanting case-insensitive or natural-order object key comparison in snapshots; copying compareKeys from a third-party pretty-format example.

Related errors


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