jestjs/jest · error · Error

${matcherHintFromConfig(...)} Received function did not thr

Error message

${matcherHintFromConfig(...)}

Received function did not throw

What it means

Thrown by the snapshot throw-matchers when the received function completed without throwing. After confirming the received value is a function and `.not` is not set, the matcher invokes `received()` inside a try/catch (index.ts:516-521); if no error was caught, `error === undefined` and this `DID_NOT_THROW` error is thrown. It is the snapshot-family equivalent of `expect(fn).toThrow()` failing.

Source

Thrown at packages/jest-snapshot/src/index.ts:525

      ),
    );
  }

  let error;

  if (fromPromise) {
    error = received;
  } else {
    try {
      received();
    } catch (receivedError) {
      error = receivedError;
    }
  }

  if (error === undefined) {
    // Because the received value is a function, this is not a matcher error.
    throw new Error(
      `${matcherHintFromConfig(config, false)}\n\n${DID_NOT_THROW}`,
    );
  }

  let message = error.message;
  while ('cause' in error) {
    error = error.cause;
    if (isError(error) || error instanceof Error) {
      message += `\nCause: ${error.message}`;
    } else {
      if (typeof error === 'string') {
        message += `\nCause: ${error}`;
      }
      break;
    }
  }

  return _toMatchSnapshot({

View on GitHub (pinned to f49721c78e)

Solutions

  1. Verify the function actually throws under the test's current conditions by debugging or logging inside it.
  2. If the function is async, ensure you pass a function that returns a rejected promise (or `await expect(fn).rejects.toThrowErrorMatchingSnapshot()`).
  3. If the function is genuinely not supposed to throw, replace the matcher with `expect(fn).not.toThrow()`.

Example fix

// before: getUser throws when id missing, but test passes a valid id
expect(() => getUser(validId)).toThrowErrorMatchingSnapshot();
// after
expect(() => getUser(undefined)).toThrowErrorMatchingSnapshot();
Defensive patterns

Strategy: validation

Validate before calling

// Prove the function throws before asserting it via the snapshot matcher
function throwsUnder(fn, setup) {
  setup?.();
  try { fn(); return false; } catch { return true; }
}
if (!throwsUnder(fn, setupState)) throw new Error('precondition: fn must throw');

Type guard

const isThrowing = (fn: () => unknown): boolean => {
  try { fn(); return false; } catch { return true; }
};

Prevention

When it happens

Trigger: Calling `expect(fn).toThrowErrorMatchingSnapshot()` or `expect(fn).toThrowErrorMatchingInlineSnapshot()` where `fn` (or the promise it resolves, via the fromPromise path) does not throw any error.

Common situations: The function under test was refactored to no longer throw; the throw is conditional on state that isn't set up in this test; an async function was passed but resolved successfully where rejection was expected; the wrong function reference was passed.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/011d556dda46e45d.json. Report an issue: GitHub.