jestjs/jest · error · Error
expected value must be a string or regular expression
Error message
expected value must be a string or regular expression
What it means
Thrown by `toMatch` (matchers.ts:816) when the expected argument is neither a string nor a regex-like object (an object with a `.test` function). `toMatch` accepts `string` (substring) or `RegExp` (pattern); anything else cannot be applied to a string. The check is duck-typed: it allows objects implementing `.test`, but rejects numbers, booleans, plain objects, null.
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 f49721c78e)
Solutions
- Wrap the value as a regex if you want pattern matching: `.toMatch(new RegExp(value))`.
- Coerce to string for substring matching: `.toMatch(String(value))`.
- Verify the imported regex constant is actually a RegExp — log `expected instanceof RegExp`.
- Use `toEqual` if you meant equality, not pattern matching.
Example fix
// before
expect(msg).toMatch(patternConfig); // patternConfig is {regex: '/x/'}
// after
expect(msg).toMatch(patternConfig.regex); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof expected !== 'string' && !(expected instanceof RegExp)) {
throw new Error(`expected must be string or RegExp, got ${typeof expected}`);
}
expect(str).toMatch(expected); Type guard
const isStringOrRegExp = (v: unknown): v is string | RegExp => typeof v === 'string' || v instanceof RegExp;
Try / catch
try {
expect(str).toMatch(expected);
} catch (e) {
if (e instanceof Error && /must be a string or regular expression/.test(e.message)) {
console.error('expected was', typeof expected, expected);
}
throw e;
} Prevention
- Import regex constants and verify `instanceof RegExp`.
- Coerce values with `String(...)` or wrap with `new RegExp(...)`.
- Use `toEqual` for equality instead of pattern matching.
When it happens
Trigger: Calling `expect(str).toMatch(123)` (number), `.toMatch(true)`, `.toMatch({pattern: 'x'})` (plain object), `.toMatch(null)`, or `.toMatch(['a','b'])` (array). Also `.toMatch(new String('x'))` would fail because a String object's `typeof` is `'object'` without a `.test`.
Common situations: Passing a token or ID number where a substring was intended; passing a config object that holds a regex instead of the regex itself; refactor that changed the expected from string to number; mis-importing a regex constant that resolved to undefined.
Related errors
- expected value must be a string if received value is a strin
- received value must be a string
- expected path must be a string or array
- expected value must be a number
- received value must be a number
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/5eb1279b31a107c9.json.
Report an issue: GitHub.