jestjs/jest · error · Error

${matcherHintFromConfig(...)} Snapshot matchers cannot be u

Error message

${matcherHintFromConfig(...)}

Snapshot matchers cannot be used with not

What it means

Thrown by `_toMatchSnapshot` (index.ts:294-301) when a snapshot matcher is combined with `.not`. Snapshots are equality checks against a stored baseline; negated snapshot assertions are meaningless (`expect(x).not.toMatchSnapshot()` would pass on any change including the desired one), so Jest rejects them outright.

Source

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

const _toMatchSnapshot = (config: MatchSnapshotConfig) => {
  const {context, hint, inlineSnapshot, isInline, matcherName, properties} =
    config;
  let {received} = config;

  /** If a test was ran with `test.failing`. Passed by Jest Circus. */
  const {testFailing = false} = context;

  if (!testFailing && context.dontThrow) {
    // Suppress errors while running tests
    context.dontThrow();
  }

  const {currentConcurrentTestName, isNot, snapshotState} = context;
  const currentTestName =
    currentConcurrentTestName?.() ?? context.currentTestName;

  if (isNot) {
    throw new Error(
      matcherErrorMessage(
        matcherHintFromConfig(config, false),
        NOT_SNAPSHOT_MATCHERS,
      ),
    );
  }

  if (snapshotState == null) {
    // Because the state is the problem, this is not a matcher error.
    // Call generic stringify from jest-matcher-utils package
    // because uninitialized snapshot state does not need snapshot serializers.
    throw new Error(
      `${matcherHintFromConfig(config, false)}\n\n` +
        'Snapshot state must be initialized' +
        `\n\n${printWithType('Snapshot state', snapshotState, stringify)}`,
    );
  }

View on GitHub (pinned to f49721c78e)

Solutions

  1. Remove `.not`: `expect(x).toMatchSnapshot()`.
  2. If you need to assert that a value differs from a known baseline, compare against the baseline explicitly with `not.toEqual(baseline)` instead of a snapshot.
  3. Update or delete the snapshot file to express the new expected value.

Example fix

// before
expect(result).not.toMatchSnapshot();

// after
expect(result).toMatchSnapshot();
// or assert against an explicit value:
expect(result).not.toBe(oldValue);
Defensive patterns

Strategy: validation

Validate before calling

// snapshots cannot be negated — drop the .not before calling
function assertSnapshot(x: unknown) {
  expect(x).toMatchSnapshot();
}

Prevention

When it happens

Trigger: `expect(x).not.toMatchSnapshot()`, `expect(fn).not.toThrowErrorMatchingSnapshot()`, `expect(x).not.toMatchInlineSnapshot()`.

Common situations: Copy-pasting a negated expect template. Misunderstanding snapshot semantics — wanting 'assert this is not yet snapshotted'.

Related errors


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