{"record":{"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":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/config/resolveConfig.ts","lineNumber":648,"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":630,"sourceCodeEnd":666,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/node/config/resolveConfig.ts#L630-L666","documentation":"Snapshot formatting options are serialised into the resolved config so they can be transferred to worker threads and the snapshot environment. A function value for `snapshotFormat.compareKeys` cannot survive that serialisation, so Vitest rejects it up front (note the TODO in source: a future API may support it via `Function.toString()` or a separate DiffOptions hook).","triggerScenarios":"Passing `test.snapshotFormat = { compareKeys: (a, b) => a.localeCompare(b) }` in a config file.","commonSituations":"Copy-pasting a jest/pretty-format example that uses a comparator function; trying to enforce custom key ordering in snapshots.","solutions":["Remove the function form of `compareKeys` from `test.snapshotFormat`.","Use the built-in string-based `compareKeys: null` (default insertion order) or a library-defined token if/when supported.","If custom ordering is essential, file an issue / await the planned DiffOptions API."],"exampleFix":"// before\ntest: { snapshotFormat: { compareKeys: (a, b) => a < b ? -1 : 1 } }\n// after\ntest: { snapshotFormat: {} }","handlingStrategy":"type-guard","validationCode":"if (config.test?.snapshotFormat && typeof config.test.snapshotFormat.compareKeys === 'function') {\n  throw new TypeError('snapshotFormat.compareKeys cannot be a function; remove it.')\n}","typeGuard":"function hasFunctionCompareKeys(fmt: any): boolean {\n  return fmt != null && typeof fmt.compareKeys === 'function'\n}","tryCatchPattern":null,"preventionTips":["Treat snapshotFormat as a serialisable, data-only object.","Track the upstream TODO for a future DiffOptions-based comparator API."],"tags":["config","snapshot","serialization","validation"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}