{"id":"c3fe2cefd0911117","repo":"jestjs/jest","slug":"pretty-format-option-theme-has-a-key-key-w","errorCode":null,"errorMessage":"pretty-format: Option \"theme\" has a key \"${key}\" whose value \"${value}\" is undefined in ansi-styles.","messagePattern":"pretty-format: Option \"theme\" has a key \"(.+?)\" whose value \"(.+?)\" is undefined in ansi-styles\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/pretty-format/src/index.ts","lineNumber":463,"sourceCode":"    }\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    ) {\n      colors[key] = color;\n    } else {\n      throw new Error(\n        `pretty-format: Option \"theme\" has a key \"${key}\" whose value \"${value}\" is undefined in ansi-styles.`,\n      );\n    }\n    return colors;\n  }, Object.create(null));\n\nconst getColorsEmpty = (): Colors =>\n  DEFAULT_THEME_KEYS.reduce((colors, key) => {\n    colors[key] = {close: '', open: ''};\n    return colors;\n  }, Object.create(null));\n\nconst getPrintFunctionName = (options?: OptionsReceived) =>\n  options?.printFunctionName ?? DEFAULT_OPTIONS.printFunctionName;\n\nconst getEscapeRegex = (options?: OptionsReceived) =>\n  options?.escapeRegex ?? DEFAULT_OPTIONS.escapeRegex;\n","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/pretty-format/src/index.ts#L445-L481","documentation":"getColorsHighlight() iterates DEFAULT_THEME_KEYS (comment, content, prop, tag, value) and resolves each value against the ansi-styles module via `(style as any)[value]`. If the resolved entry has no string `open`/`close` properties, the value is not a real ansi-styles color and this Error is thrown at index.ts:463. It protects the printer from emitting broken escape sequences.","triggerScenarios":"Passing { highlight: true, theme: { tag: 'mauve' } } where 'mauve' is not an ansi-styles export; typos like { prop: 'yelow' }; passing a hex code or CSS color string ('#ff0') instead of a named ansi-styles token.","commonSituations":"Developers assume any CSS/html color name works, type a color name from memory that isn't in ansi-styles, or copy a theme from a different highlighting library whose palette names differ from ansi-styles.","solutions":["Use only ansi-styles color/style names recognized by the ansi-styles package (e.g. 'red', 'cyan', 'gray', 'bold', 'reset', 'yellow', 'green', 'magenta', 'blue').","Omit the specific key to fall back to DEFAULT_THEME for that key (only override keys you need).","If unsure, validate the value against `require('ansi-styles')` before passing it as a theme value."],"exampleFix":"// before\nformat(obj, { highlight: true, theme: { tag: 'mauve', prop: '#ff0' } });\n// after\nformat(obj, { highlight: true, theme: { tag: 'cyan', prop: 'yellow' } });","handlingStrategy":"validation","validationCode":"const ansiStyles = require('ansi-styles');\nconst ALLOWED_KEYS = ['comment', 'content', 'prop', 'tag', 'value'];\nfunction validateTheme(theme) {\n  if (!theme) return;\n  for (const [k, v] of Object.entries(theme)) {\n    if (!ALLOWED_KEYS.includes(k)) continue;\n    const entry = ansiStyles[v];\n    if (!entry || typeof entry.open !== 'string' || typeof entry.close !== 'string') {\n      throw new Error(`Invalid ansi-styles color for '${k}': '${v}'`);\n    }\n  }\n}","typeGuard":"function isValidAnsiColor(name, ansiStyles): boolean {\n  const e = ansiStyles[name];\n  return !!e && typeof e.open === 'string' && typeof e.close === 'string';\n}","tryCatchPattern":null,"preventionTips":["Cross-check each theme value against the ansi-styles module before passing it.","Only use recognized ansi-styles names; CSS/html colors and hex codes are not supported.","Override only the theme keys you need; unset keys fall back to DEFAULT_THEME safely."],"tags":["pretty-format","theme","ansi-styles","validation","highlight"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}