jestjs/jest · error · Error
${EXPECTED_COLOR('expected')} value must be a string or regu
Error message
${EXPECTED_COLOR('expected')} value must be a string or regular expression What it means
Thrown by `toMatch` (matchers.ts:836) when the expected value is neither a string nor a regex-like object (something with a `.test` function). The matcher branches on string (substring) vs RegExp (pattern); anything else (number, object without test, null, undefined, array) is rejected.
Source
Thrown at packages/expect/src/matchers.ts:837
isNot: this.isNot,
promise: this.promise,
};
if (typeof received !== 'string') {
throw new TypeError(
matcherErrorMessage(
matcherHint(matcherName, undefined, undefined, options),
`${RECEIVED_COLOR('received')} value must be a string`,
printWithType('Received', received, printReceived),
),
);
}
if (
!(typeof expected === 'string') &&
!(expected && typeof expected.test === 'function')
) {
throw new Error(
matcherErrorMessage(
matcherHint(matcherName, undefined, undefined, options),
`${EXPECTED_COLOR(
'expected',
)} value must be a string or regular expression`,
printWithType('Expected', expected, printExpected),
),
);
}
const pass =
typeof expected === 'string'
? received.includes(expected)
: new RegExp(expected).test(received);
const message = pass
? () =>
typeof expected === 'string'View on GitHub (pinned to 8e6d128e4a)
Solutions
- Pass a string substring or a RegExp: toMatch('hel') or toMatch(/^hel/).
- Convert a config pattern string into a RegExp when pattern semantics are needed: new RegExp(pattern).
- Ensure the expected variable is defined (a failed lookup gives undefined).
- Do not pass plain objects or numbers; use the correct matcher for those types.
Example fix
// before
expect(text).toMatch('*error*'); // treated as literal substring
expect(text).toMatch(404);
// after
expect(text).toMatch('error');
expect(text).toMatch(/error/);
// convert a glob-ish config
expect(text).toMatch(new RegExp(configPattern)); Defensive patterns
Strategy: type-guard
Validate before calling
const expected = pattern;
if (typeof expected !== 'string' && !(expected instanceof RegExp)) {
throw new Error('toMatch expected must be a string or RegExp');
}
expect(text).toMatch(expected); Type guard
const isStringOrRegExp = (v: unknown): v is string | RegExp => typeof v === 'string' || v instanceof RegExp;
Prevention
- Wrap config pattern strings in new RegExp() when you need regex semantics.
- Keep substring (string) vs pattern (RegExp) intentions explicit.
- Ensure the expected variable is defined.
When it happens
Trigger: Calling `expect('hello').toMatch(5)` (number), `expect('hello').toMatch({pattern: 'h'})` (plain object), `expect('hello').toMatch(null)`, or omitting the expected so it is undefined.
Common situations: Passing a glob or pattern string from a config and forgetting it must be a RegExp for pattern semantics; passing an object by mistake; a variable that is undefined because a lookup failed.
Related errors
- ${RECEIVED_COLOR('received')} value must be a string
- ${EXPECTED_COLOR('expected')} value must be a number
- ${RECEIVED_COLOR('received')} value must be a number
- ${EXPECTED_COLOR('expected')} value must be a function
- ${RECEIVED_COLOR('received')} value must not be null nor und
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/82e64674b1423858.
Report an issue: GitHub.