{"record":{"id":"82ee010691843758","repo":"avajs/ava","slug":"t-snapshot-can-only-be-used-in-tests","errorCode":null,"errorMessage":"`t.snapshot()` can only be used in tests","messagePattern":"`t\\.snapshot\\(\\)` can only be used in tests","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"lib/assert.js","lineNumber":636,"sourceCode":"\t\t\t\t\tassertion: 't.notThrowsAsync()',\n\t\t\t\t\tcause: error,\n\t\t\t\t\tformattedDetails: [formatWithLabel('Function threw:', error)],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\tif (!isPromise(retval)) {\n\t\t\t\tthrow fail(new AssertionError(message, {\n\t\t\t\t\tassertion: 't.notThrowsAsync()',\n\t\t\t\t\tformattedDetails: [formatWithLabel('Function did not return a promise. Use `t.notThrows()` instead:', retval)],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\treturn handlePromise(retval, true);\n\t\t});\n\n\t\tthis.snapshot = withSkip((expected, message) => {\n\t\t\tif (disableSnapshots) {\n\t\t\t\tthrow fail(new AssertionError('`t.snapshot()` can only be used in tests', {\n\t\t\t\t\tassertion: 't.snapshot()',\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\tassertMessage(message, 't.snapshot()');\n\n\t\t\tif (message === '') {\n\t\t\t\tthrow fail(new AssertionError('The snapshot assertion message must be a non-empty string', {\n\t\t\t\t\tassertion: 't.snapshot()',\n\t\t\t\t\tformattedDetails: [formatWithLabel('Called with:', message)],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\tlet result;\n\t\t\ttry {\n\t\t\t\tresult = compareWithSnapshot({expected, message});\n\t\t\t} catch (error) {\n\t\t\t\tif (!(error instanceof SnapshotError)) {","sourceCodeStart":618,"sourceCodeEnd":654,"githubUrl":"https://github.com/avajs/ava/blob/bbfd946322fdeca2b547a691d947fb4e18c0c67f/lib/assert.js#L618-L654","documentation":"AVA's t.snapshot() records/compares snapshot files, but snapshots are only enabled inside test runs where a snapshot manager is wired up. When the assertion runs in a context where snapshots are disabled (e.g. inside a worker without snapshot support, or a custom/non-test execution context), AVA throws this AssertionError to prevent writing snapshots incorrectly.","triggerScenarios":"Calling t.snapshot() in code shared between tests and non-test contexts where the assertion context was created with snapshots disabled; using t.snapshot() in setup/teardown helpers or in AVA's experimental contexts that disallow it; invoking test file code outside `ava` (e.g. via node directly) with a manually constructed assertion context.","commonSituations":"Shared assertion utilities used by both unit tests and scripts; running test files under a different runner or bundler that bypasses AVA's snapshot setup; snapshot assertions inside hooks or sub-workflows where the disableSnapshots flag is true.","solutions":["Only call t.snapshot() inside regular test bodies executed by AVA","Move snapshot assertions out of shared helpers/hooks into test bodies","If the code must run outside tests, gate it: only snapshot when the context supports it","Update AVA if using an older version with different snapshot context rules"],"exampleFix":"// before\n// shared helper called from tests and scripts\nexport function verify(t, value) {\n  t.snapshot(value);\n}\n// after\nexport function verify(t, value) {\n  if (typeof t.snapshot === 'function' && t.snapshotEnabled !== false) {\n    t.snapshot(value);\n  } else {\n    t.deepEqual(value, expectedValue);\n  }\n}","handlingStrategy":"validation","validationCode":"// Only snapshot in AVA test bodies, never in shared helpers invoked outside tests:\nif (typeof t.snapshot !== 'function' || snapshotsDisabled) {\n  t.deepEqual(actual, expected); // fallback assertion\n} else {\n  t.snapshot(actual);\n}","typeGuard":"const canSnapshot = (t) => typeof t.snapshot === 'function' && !t.snapshotDisabled;","tryCatchPattern":"try {\n  t.snapshot(value);\n} catch (e) {\n  if (/can only be used in tests/.test(e.message)) {\n    t.deepEqual(value, expected);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Keep t.snapshot() calls inside AVA test bodies only","Do not share assertion contexts between tests and scripts/CLIs","Run test files through the ava runner, not directly with node","Document snapshot restrictions in shared test utilities"],"tags":["ava","snapshot","assertion","usage"],"backgroundTag":"snapshot-outside-test","analyzedSha":"bbfd946322fdeca2b547a691d947fb4e18c0c67f","analyzedAt":"2026-09-02T01:24:43.834Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}