jestjs/jest · error · TypeError
pretty-format: Option "theme" must be of type "object" but i
Error message
pretty-format: Option "theme" must be of type "object" but instead received "${typeof options.theme}". What it means
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.
Source
Thrown at packages/pretty-format/src/index.ts:442
for (const key of Object.keys(options)) {
if (!Object.prototype.hasOwnProperty.call(DEFAULT_OPTIONS, key)) {
throw new Error(`pretty-format: Unknown option "${key}".`);
}
}
if (options.min && options.indent !== undefined && options.indent !== 0) {
throw new Error(
'pretty-format: Options "min" and "indent" cannot be used together.',
);
}
if (options.theme !== undefined) {
if (options.theme === null) {
throw new Error('pretty-format: Option "theme" must not be null.');
}
if (typeof options.theme !== 'object') {
throw new TypeError(
`pretty-format: Option "theme" must be of type "object" but instead received "${typeof options.theme}".`,
);
}
}
}
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'
) {View on GitHub (pinned to f49721c78e)
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().
Example fix
// before
format(obj, { highlight: true, theme: 'dark' });
// after
format(obj, { highlight: true, theme: { tag: 'cyan', content: 'reset' } }); Defensive patterns
Strategy: validation
Validate before calling
function safeTheme(theme) {
if (theme === undefined) return undefined;
if (theme === null || typeof theme !== 'object') {
throw new TypeError('theme must be an object mapping {comment,content,prop,tag,value} to ansi-styles names');
}
return theme;
}
// then: format(value, { highlight: true, theme: safeTheme(maybeTheme) }); Type guard
function isThemeObject(v): v is { comment?: string; content?: string; prop?: string; tag?: string; value?: string } {
return v === undefined || (typeof v === 'object' && v !== null && !Array.isArray(v));
} Prevention
- 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().
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- pretty-format: Option "theme" has a key "${key}" whose value
- Configuration in ${packageJson} is not valid. Jest expects t
- Multiple configurations found Implicit config resolution d
- For a percentage based memory limit a percentageReference mu
- Unexpected numerical input
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/0acddeae8912153d.json.
Report an issue: GitHub.