jestjs/jest · error · Error
pretty-format: Option "theme" has a key "${key}" whose value
Error message
pretty-format: Option "theme" has a key "${key}" whose value "${value}" is undefined in ansi-styles. What it means
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.
Source
Thrown at packages/pretty-format/src/index.ts:463
}
}
}
const getColorsHighlight = (options: OptionsReceived): Colors =>
DEFAULT_THEME_KEYS.reduce((colors, key) => {
const value =
options.theme && options.theme[key] !== undefined
? options.theme[key]
: DEFAULT_THEME[key];
const color = value && (style as any)[value];
if (
color &&
typeof color.close === 'string' &&
typeof color.open === 'string'
) {
colors[key] = color;
} else {
throw new Error(
`pretty-format: Option "theme" has a key "${key}" whose value "${value}" is undefined in ansi-styles.`,
);
}
return colors;
}, Object.create(null));
const getColorsEmpty = (): Colors =>
DEFAULT_THEME_KEYS.reduce((colors, key) => {
colors[key] = {close: '', open: ''};
return colors;
}, Object.create(null));
const getPrintFunctionName = (options?: OptionsReceived) =>
options?.printFunctionName ?? DEFAULT_OPTIONS.printFunctionName;
const getEscapeRegex = (options?: OptionsReceived) =>
options?.escapeRegex ?? DEFAULT_OPTIONS.escapeRegex;
View on GitHub (pinned to f49721c78e)
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.
Example fix
// before
format(obj, { highlight: true, theme: { tag: 'mauve', prop: '#ff0' } });
// after
format(obj, { highlight: true, theme: { tag: 'cyan', prop: 'yellow' } }); Defensive patterns
Strategy: validation
Validate before calling
const ansiStyles = require('ansi-styles');
const ALLOWED_KEYS = ['comment', 'content', 'prop', 'tag', 'value'];
function validateTheme(theme) {
if (!theme) return;
for (const [k, v] of Object.entries(theme)) {
if (!ALLOWED_KEYS.includes(k)) continue;
const entry = ansiStyles[v];
if (!entry || typeof entry.open !== 'string' || typeof entry.close !== 'string') {
throw new Error(`Invalid ansi-styles color for '${k}': '${v}'`);
}
}
} Type guard
function isValidAnsiColor(name, ansiStyles): boolean {
const e = ansiStyles[name];
return !!e && typeof e.open === 'string' && typeof e.close === 'string';
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- pretty-format: Option "theme" must be of type "object" but i
- @jest/diff-sequences: ${name} typeof ${typeof arg} is not a
- @jest/diff-sequences: ${name} value ${arg} is not a safe int
- @jest/diff-sequences: ${name} value ${arg} is a negative int
- @jest/diff-sequences: ${name} typeof ${type} is not a functi
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/c3fe2cefd0911117.json.
Report an issue: GitHub.