jestjs/jest · error · Error
${matcherHint(...)} received value must be a function ${prin
Error message
${matcherHint(...)} received value must be a function
${printWithType('Received', received, printReceived)} What it means
Thrown by `_toThrowErrorMatchingSnapshot` (index.ts:488-499) when `received` is not a function and the call didn't come through the promise path. These matchers wrap a callable to catch its thrown error; passing a non-function (and not resolving via `fromPromise`) means there's nothing to invoke, so Jest surfaces a typed matcher error.
Source
Thrown at packages/jest-snapshot/src/index.ts:492
);
};
const _toThrowErrorMatchingSnapshot = (
config: MatchSnapshotConfig,
fromPromise?: boolean,
) => {
const {context, hint, inlineSnapshot, isInline, matcherName, received} =
config;
context.dontThrow?.();
const {isNot, promise} = context;
if (!fromPromise) {
if (typeof received !== 'function') {
const options: MatcherHintOptions = {isNot, promise};
throw new Error(
matcherErrorMessage(
matcherHint(matcherName, undefined, '', options),
`${RECEIVED_COLOR('received')} value must be a function`,
printWithType('Received', received, printReceived),
),
);
}
}
if (isNot) {
throw new Error(
matcherErrorMessage(
matcherHintFromConfig(config, false),
NOT_SNAPSHOT_MATCHERS,
),
);
}
View on GitHub (pinned to f49721c78e)
Solutions
- Pass a function: `expect(() => risky()).toThrowErrorMatchingSnapshot()`.
- If asserting on a promise rejection, use the async form or ensure the matcher receives `fromPromise: true` via the runner.
- For an already-caught error, use `toMatchSnapshot` on the error object instead.
Example fix
// before
expect(parse('bad')).toThrowErrorMatchingSnapshot(); // parse runs first
// after
expect(() => parse('bad')).toThrowErrorMatchingSnapshot(); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof received !== 'function') {
throw new Error('Pass a function: expect(() => fn()).toThrowErrorMatchingSnapshot()');
}
expect(received).toThrowErrorMatchingSnapshot(); Type guard
function isFunction(v: unknown): v is Function {
return typeof v === 'function';
} Prevention
- Always wrap throwing expressions in an arrow function so they're invoked by the matcher.
- For promise rejections, use the async matcher path so `fromPromise` is set.
- Lint against `expect(*).toThrow*` where `*` is not a function literal or arrow.
When it happens
Trigger: `expect('boom').toThrowErrorMatchingSnapshot()`, `expect(42).toThrowErrorMatchingInlineSnapshot()`, `expect(null).toThrowErrorMatchingSnapshot()`.
Common situations: Forgetting to wrap the throwing code in a function: `expect(risky()).toThrowErrorMatchingSnapshot()` evaluates `risky()` first and passes its result. Asserting against an already-caught error object instead of a callable.
Related errors
- ${matcherHint(...)} Expected properties must be an object ${
- ${matcherHint(...)} Expected properties must be an object ${
- ${matcherHint(...)} Inline snapshot must be a string ${print
- ${matcherHintFromConfig(...)} received value must be an obje
- ${matcherHintFromConfig(...)} Snapshot matchers cannot be u
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/b06c51a654d8bbbf.json.
Report an issue: GitHub.