{"id":"0acddeae8912153d","repo":"jestjs/jest","slug":"pretty-format-option-theme-must-be-of-type-obj","errorCode":null,"errorMessage":"pretty-format: Option \"theme\" must be of type \"object\" but instead received \"${typeof options.theme}\".","messagePattern":"pretty-format: Option \"theme\" must be of type \"object\" but instead received \"(.+?)\"\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/pretty-format/src/index.ts","lineNumber":442,"sourceCode":"  for (const key of Object.keys(options)) {\n    if (!Object.prototype.hasOwnProperty.call(DEFAULT_OPTIONS, key)) {\n      throw new Error(`pretty-format: Unknown option \"${key}\".`);\n    }\n  }\n\n  if (options.min && options.indent !== undefined && options.indent !== 0) {\n    throw new Error(\n      'pretty-format: Options \"min\" and \"indent\" cannot be used together.',\n    );\n  }\n\n  if (options.theme !== undefined) {\n    if (options.theme === null) {\n      throw new Error('pretty-format: Option \"theme\" must not be null.');\n    }\n\n    if (typeof options.theme !== 'object') {\n      throw new TypeError(\n        `pretty-format: Option \"theme\" must be of type \"object\" but instead received \"${typeof options.theme}\".`,\n      );\n    }\n  }\n}\n\nconst getColorsHighlight = (options: OptionsReceived): Colors =>\n  DEFAULT_THEME_KEYS.reduce((colors, key) => {\n    const value =\n      options.theme && options.theme[key] !== undefined\n        ? options.theme[key]\n        : DEFAULT_THEME[key];\n    const color = value && (style as any)[value];\n    if (\n      color &&\n      typeof color.close === 'string' &&\n      typeof color.open === 'string'\n    ) {","sourceCodeStart":424,"sourceCodeEnd":460,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/pretty-format/src/index.ts#L424-L460","documentation":"pretty-format's validateOptions() runs on every format()/printer call and asserts the `theme` option (used for ANSI syntax highlighting) is a plain object. After separately rejecting null at index.ts:437, it checks `typeof options.theme !== 'object'`; any other primitive (string, number, boolean, function) raises this TypeError so getColorsHighlight never reads a property off a non-object. Valid theme keys are comment, content, prop, tag, value.","triggerScenarios":"Calling format(value, { highlight: true, theme: 'dark' }), format(value, { theme: 2 }), or format(value, { theme: true }) — any invocation where options.theme is a primitive other than object/null. Passing a preset name string instead of a {key: colorName} object is the canonical trigger.","commonSituations":"Developers confuse `highlight` (boolean toggle) with `theme` (the color map object), pass a named preset string, or spread a partial config where theme gets coerced to a primitive. Also seen when porting snapshot serializers that accept a string color preset.","solutions":["Pass theme as an object mapping the five keys (comment, content, prop, tag, value) to ansi-styles color names: { theme: { tag: 'cyan', content: 'reset' } }.","If you only want default colors, omit theme entirely and use { highlight: true } to get DEFAULT_THEME.","When building options dynamically, guard first: ensure typeof options.theme === 'object' && options.theme !== null before calling format()."],"exampleFix":"// before\nformat(obj, { highlight: true, theme: 'dark' });\n// after\nformat(obj, { highlight: true, theme: { tag: 'cyan', content: 'reset' } });","handlingStrategy":"validation","validationCode":"function safeTheme(theme) {\n  if (theme === undefined) return undefined;\n  if (theme === null || typeof theme !== 'object') {\n    throw new TypeError('theme must be an object mapping {comment,content,prop,tag,value} to ansi-styles names');\n  }\n  return theme;\n}\n// then: format(value, { highlight: true, theme: safeTheme(maybeTheme) });","typeGuard":"function isThemeObject(v): v is { comment?: string; content?: string; prop?: string; tag?: string; value?: string } {\n  return v === undefined || (typeof v === 'object' && v !== null && !Array.isArray(v));\n}","tryCatchPattern":null,"preventionTips":["Never pass a string preset as theme; theme is always a {key: colorName} object.","Treat highlight (boolean) and theme (object) as separate options and don't conflate them.","When building options dynamically, coerce with a guard so a stray primitive never reaches format()."],"tags":["pretty-format","config","validation","theme","ansi-styles"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}