vitest-dev/vitest · 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

When `highlight: true`, pretty-format resolves each theme color name (comment, content, prop, tag, value) to a style function on the `tinyrainbow` dependency. If a configured color value has no matching entry in tinyrainbow, `getColorsHighlight` throws reporting the offending key/value. In practice this is a dependency/version mismatch rather than user error, since the code resolves the default theme keys.

Source

Thrown at packages/pretty-format/src/index.ts:494

    throw new Error(
      'pretty-format: Options "min" and "indent" cannot be used together.',
    )
  }
}

function getColorsHighlight(): Colors {
  return DEFAULT_THEME_KEYS.reduce((colors, key) => {
    const value = DEFAULT_THEME[key]
    const color = value && (styles 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))
}

function getColorsEmpty(): Colors {
  return DEFAULT_THEME_KEYS.reduce((colors, key) => {
    colors[key] = { close: '', open: '' }
    return colors
  }, Object.create(null))
}

function getPrintFunctionName(options?: OptionsReceived) {
  return options?.printFunctionName ?? DEFAULT_OPTIONS.printFunctionName
}

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Ensure a compatible `tinyrainbow` version is installed and hoisted (one that exports gray/reset/yellow/cyan/green).
  2. Disable highlighting: `format(value, { highlight: false })`.
  3. Run `pnpm why tinyrainbow` / `npm ls tinyrainbow` to find and resolve version conflicts.

Example fix

// before — triggers when tinyrainbow lacks a color
format(value, { highlight: true })

// after — bypass color resolution
format(value, { highlight: false })
Defensive patterns

Strategy: validation

Validate before calling

import styles from 'tinyrainbow'
import { DEFAULT_THEME } from '@vitest/pretty-format'

function assertThemeColorsAvailable() {
  for (const [key, value] of Object.entries(DEFAULT_THEME)) {
    if (value && typeof (styles as any)[value]?.open !== 'string') {
      throw new Error(`tinyrainbow is missing color '${value}' (theme key '${key}'). Fix dependency or disable highlight.`)
    }
  }
}

Try / catch

let output: string
try {
  output = format(value, { highlight: true })
}
catch (err) {
  if (err instanceof Error && /is undefined in ansi-styles/.test(err.message)) {
    // tinyrainbow version mismatch — fall back to no color
    output = format(value, { highlight: false })
  } else {
    throw err
  }
}

Prevention

When it happens

Trigger: `format(value, { highlight: true })` on a system where the installed `tinyrainbow` lacks one of the resolved color names (e.g. a major version removed/renamed `gray` or `reset`), or a build that patches `DEFAULT_THEME` to an unsupported value.

Common situations: tinyrainbow major upgrade changing the color API; dedupe/hoisting picking an incompatible tinyrainbow version in a monorepo; custom fork of pretty-format with an altered theme.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/587ee99236e0acc8.json. Report an issue: GitHub.