{"id":"9d9f4acbe6e666f6","repo":"vitest-dev/vitest","slug":"invalid-snapshot-serializer-in-file-must-have","errorCode":null,"errorMessage":"invalid snapshot serializer in ${file}. Must have a 'test' method along with either a 'serialize' or 'print' method.","messagePattern":"invalid snapshot serializer in (.+?)\\. Must have a 'test' method along with either a 'serialize' or 'print' method\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/setup-common.ts","lineNumber":85,"sourceCode":"): Promise<void> {\n  const files = config.snapshotSerializers\n\n  const snapshotSerializers = await Promise.all(\n    files.map(async (file) => {\n      const mo = await moduleRunner.import(file)\n      if (!mo || typeof mo.default !== 'object' || mo.default === null) {\n        throw new Error(\n          `invalid snapshot serializer file ${file}. Must export a default object`,\n        )\n      }\n\n      const config = mo.default\n      if (\n        typeof config.test !== 'function'\n        || (typeof config.serialize !== 'function'\n          && typeof config.print !== 'function')\n      ) {\n        throw new TypeError(\n          `invalid snapshot serializer in ${file}. Must have a 'test' method along with either a 'serialize' or 'print' method.`,\n        )\n      }\n\n      return config as SnapshotSerializer\n    }),\n  )\n\n  snapshotSerializers.forEach(serializer => addSerializer(serializer))\n}\n","sourceCodeStart":67,"sourceCodeEnd":96,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/setup-common.ts#L67-L96","documentation":"Thrown by loadSnapshotSerializers (as a TypeError) when the serializer's default object exists but does not conform to the required interface: it must have a `test` function AND either a `serialize` or a `print` function. These mirror Jest's snapshot serializer contract used by @vitest/snapshot.","triggerScenarios":"A serializer default object that has `serialize` but no `test`. One that has `test` but neither `serialize` nor `print`. A serializer that exports `{ print }` only (no test). Misnaming the methods (e.g. `serializeValue` instead of `serialize`).","commonSituations":"Hand-writing a serializer from memory and forgetting `test`. Copying a partial example. Renaming methods during a refactor. Confusing this with the expect.addSnapshotSerializer inline shape.","solutions":["Ensure the default object has `test(val): boolean`.","Add exactly one of `serialize(val, config, indent, depth, refs, printer)` or `print(val, printer)` with those exact names.","Double-check method spelling: it is `serialize`/`print`, not `serialise`/`printValue`.","Reference Jest's snapshot serializer docs - Vitest mirrors that API."],"exampleFix":"// before\nexport default {\n  serialize: (val) => `Foo(${val})`,\n  // missing test\n}\n\n// after\nexport default {\n  test: val => val?.constructor?.name === 'Foo',\n  serialize: (val, config, indent, depth, refs, printer) => `Foo(${printer(val.value, config, indent, depth, refs)})`,\n}","handlingStrategy":"type-guard","validationCode":"import serializerDefault from './my-serializer.js'\nconst s = serializerDefault as any\nif (typeof s.test !== 'function' || (typeof s.serialize !== 'function' && typeof s.print !== 'function')) {\n  throw new Error('serializer must have test() and serialize()|print()')\n}","typeGuard":"function isSerializer(v: unknown): v is { test: Function; serialize?: Function; print?: Function } {\n  if (!v || typeof v !== 'object') return false\n  const o = v as any\n  return typeof o.test === 'function' && (typeof o.serialize === 'function' || typeof o.print === 'function')\n}","tryCatchPattern":null,"preventionTips":["Always include `test` and exactly one of serialize/print.","Keep method names exactly test/serialize/print.","Mirror the Jest snapshot serializer API to stay compatible."],"tags":["config","snapshot","serializer","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}