{"id":"4a299ace7299995f","repo":"jestjs/jest","slug":"matcherhintfromconfig-snapshot-state-must","errorCode":null,"errorMessage":"${matcherHintFromConfig(...)}\n\nSnapshot state must be initialized\n\n${printWithType('Snapshot state', snapshotState, stringify)}","messagePattern":"(.+?)\n\nSnapshot state must be initialized\n\n(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-snapshot/src/index.ts","lineNumber":307,"sourceCode":"\n  const {currentConcurrentTestName, isNot, snapshotState} = context;\n  const currentTestName =\n    currentConcurrentTestName?.() ?? context.currentTestName;\n\n  if (isNot) {\n    throw new Error(\n      matcherErrorMessage(\n        matcherHintFromConfig(config, false),\n        NOT_SNAPSHOT_MATCHERS,\n      ),\n    );\n  }\n\n  if (snapshotState == null) {\n    // Because the state is the problem, this is not a matcher error.\n    // Call generic stringify from jest-matcher-utils package\n    // because uninitialized snapshot state does not need snapshot serializers.\n    throw new Error(\n      `${matcherHintFromConfig(config, false)}\\n\\n` +\n        'Snapshot state must be initialized' +\n        `\\n\\n${printWithType('Snapshot state', snapshotState, stringify)}`,\n    );\n  }\n\n  const fullTestName =\n    currentTestName && hint\n      ? `${currentTestName}: ${hint}`\n      : currentTestName || ''; // future BREAKING change: || hint\n\n  if (typeof properties === 'object') {\n    if (typeof received !== 'object' || received === null) {\n      throw new Error(\n        matcherErrorMessage(\n          matcherHintFromConfig(config, false),\n          `${RECEIVED_COLOR(\n            'received',","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-snapshot/src/index.ts#L289-L325","documentation":"Thrown by `_toMatchSnapshot` (index.ts:303-312) when the matcher's `context.snapshotState` is `null`/`undefined`. `SnapshotState` is created per test file by Jest's runner and threaded into the matcher context; a missing state means the matcher was invoked outside Jest's snapshot lifecycle (or the runner failed to initialize it).","triggerScenarios":"Invoking `toMatchSnapshot` outside a real Jest test (e.g. extracted into a script that calls the matcher directly), or calling it before `SnapshotState` is attached to the context. Custom runners that don't set up `snapshotState`.","commonSituations":"Unit-testing the matcher in isolation. Custom test frameworks reusing Jest matchers without bootstrapping `SnapshotState`. Race conditions in custom runners.","solutions":["Use `toMatchSnapshot` only inside real Jest test files so the runner sets up `snapshotState`.","If invoking programmatically, construct a `SnapshotState` (from `jest-snapshot`) and attach it to the matcher context before calling.","Switch to a non-snapshot assertion (`toEqual`, `toBe`) for contexts where snapshot state isn't available."],"exampleFix":"// before — outside Jest\nconst { toMatchSnapshot } = require('jest-snapshot');\ntoMatchSnapshot.call({ isNot: false }, value); // snapshotState missing\n\n// after — provide a SnapshotState\nconst { SnapshotState } = require('jest-snapshot');\nconst state = new SnapshotState('/tmp/x.snap', { update: 'all', ... });\ntoMatchSnapshot.call({ isNot: false, snapshotState: state }, value);","handlingStrategy":"validation","validationCode":"if (!context || !context.snapshotState) {\n  throw new Error('SnapshotState not initialized; run inside a Jest test or construct one.');\n}\nexpect(x).toMatchSnapshot();","typeGuard":"function hasSnapshotState(ctx: unknown): ctx is { snapshotState: import('jest-snapshot').SnapshotState } {\n  return !!ctx && typeof ctx === 'object' && 'snapshotState' in ctx && !!(ctx as any).snapshotState;\n}","tryCatchPattern":null,"preventionTips":["Only invoke snapshot matchers inside real Jest test bodies.","If driving matchers programmatically, build a SnapshotState and attach it to the matcher context.","For non-Jest contexts, prefer plain equality matchers."],"tags":["snapshot","matcher","lifecycle","custom-runner"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}