{"id":"e87d7e4fb6881ded","repo":"vitest-dev/vitest","slug":"snapshotformat-comparekeys-function-is-not-suppo","errorCode":null,"errorMessage":"\"snapshotFormat.compareKeys\" function is not supported.","messagePattern":"\"snapshotFormat\\.compareKeys\" function is not supported\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/config/resolveConfig.ts","lineNumber":620,"sourceCode":"\n  if (resolved.snapshotEnvironment) {\n    resolved.snapshotEnvironment = resolvePath(\n      resolved.snapshotEnvironment,\n      resolved.root,\n    )\n  }\n\n  resolved.testNamePattern = resolved.testNamePattern\n    ? resolved.testNamePattern instanceof RegExp\n      ? resolved.testNamePattern\n      : new RegExp(resolved.testNamePattern)\n    : undefined\n\n  if (resolved.snapshotFormat && 'plugins' in resolved.snapshotFormat) {\n    (resolved.snapshotFormat as any).plugins = []\n    // TODO: support it via separate config (like DiffOptions) or via `Function.toString()`\n    if (typeof resolved.snapshotFormat.compareKeys === 'function') {\n      throw new TypeError(`\"snapshotFormat.compareKeys\" function is not supported.`)\n    }\n  }\n\n  const UPDATE_SNAPSHOT = resolved.update || process.env.UPDATE_SNAPSHOT\n  resolved.snapshotOptions = {\n    expand: resolved.expandSnapshotDiff ?? false,\n    snapshotFormat: resolved.snapshotFormat || {},\n    updateSnapshot:\n      UPDATE_SNAPSHOT === 'all' || UPDATE_SNAPSHOT === 'new' || UPDATE_SNAPSHOT === 'none'\n        ? UPDATE_SNAPSHOT\n        : isCI && !UPDATE_SNAPSHOT ? 'none' : UPDATE_SNAPSHOT ? 'all' : 'new',\n    resolveSnapshotPath: options.resolveSnapshotPath,\n    // resolved inside the worker\n    snapshotEnvironment: null as any,\n  }\n\n  resolved.snapshotSerializers ??= []\n  resolved.snapshotSerializers = resolved.snapshotSerializers.map(file =>","sourceCodeStart":602,"sourceCodeEnd":638,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/node/config/resolveConfig.ts#L602-L638","documentation":"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.","triggerScenarios":"Set `snapshotFormat: { compareKeys: (a, b) => a.localeCompare(b) }` in vitest.config.ts.","commonSituations":"Wanting case-insensitive or natural-order object key comparison in snapshots; copying compareKeys from a third-party pretty-format example.","solutions":["Remove the function form of compareKeys; rely on the default string comparison.","If you need custom key ordering, sort object keys before snapshotting rather than via compareKeys.","Track the TODO in the source (line 618): a serializable form may be added later; for now functions are unsupported."],"exampleFix":"// before\nexport default defineConfig({ test: { snapshotFormat: { compareKeys: (a, b) => a.toLowerCase().localeCompare(b.toLowerCase()) } } })\n// after\nexport default defineConfig({ test: { snapshotFormat: {} } })","handlingStrategy":"type-guard","validationCode":"function assertNoFunctionCompareKeys(snapshotFormat: any) {\n  if (snapshotFormat && typeof snapshotFormat.compareKeys === 'function') {\n    throw new Error('snapshotFormat.compareKeys cannot be a function in config.')\n  }\n}","typeGuard":"function isSnapshotFormatSerializable(snapshotFormat: any): boolean {\n  return !(snapshotFormat && typeof snapshotFormat.compareKeys === 'function')\n}","tryCatchPattern":null,"preventionTips":["Do not pass functions inside snapshotFormat in config.","Sort object keys before snapshotting if you need custom ordering.","Track the upstream TODO for a future serializable form."],"tags":["config","snapshot","serialization","worker-threads"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}