{"record":{"id":"e4f6733187030be1","repo":"vitest-dev/vitest","slug":"getassertionname-obj-cannot-be-used-without","errorCode":null,"errorMessage":"'${getAssertionName(obj)}' cannot be used without test context","messagePattern":"'(.+?)' cannot be used without test context","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/snapshot/chai.ts","lineNumber":68,"sourceCode":"  return {\n    filepath: test.file.filepath,\n    name: getNames(test).slice(1).join(' > '),\n    testId: test.id,\n  }\n}\n\nfunction getAssertionName(assertion: Chai.Assertion): string {\n  const name = chai.util.flag(assertion, '_name') as string | undefined\n  if (!name) {\n    throw new Error('Assertion name is not set. This is a bug in Vitest. Please, open a new issue with reproduction.')\n  }\n  return name\n}\n\nfunction getTest(obj: Chai.Assertion) {\n  const test = chai.util.flag(obj, 'vitest-test') as Test | undefined\n  if (!test) {\n    throw new Error(`'${getAssertionName(obj)}' cannot be used without test context`)\n  }\n  if (test.fails) {\n    throw new TestSyntaxError(`'${getAssertionName(obj)}' cannot be used with 'test.fails'`)\n  }\n  return test\n}\n\nfunction validateAssertion(assertion: Chai.Assertion): void {\n  if (chai.util.flag(assertion, 'negate')) {\n    throw new Error(`${getAssertionName(assertion)} cannot be used with \"not\"`)\n  }\n}\n\nexport const SnapshotPlugin: ChaiPlugin = (chai, utils) => {\n  for (const key of ['matchSnapshot', 'toMatchSnapshot']) {\n    utils.addMethod(\n      chai.Assertion.prototype,\n      key,","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/integrations/snapshot/chai.ts#L50-L86","documentation":"Thrown by `getTest` when a snapshot assertion (`toMatchSnapshot`, `toMatchInlineSnapshot`, `toMatchFileSnapshot`, `toThrowErrorMatchingSnapshot[InlineSnapshot]`) is called but the `vitest-test` flag is missing from the Chai assertion object. Vitest attaches the active `Test` instance to that flag at assertion-creation time; without it the snapshot cannot be attributed to a test file/name, so the call is rejected. The message names the offending assertion.","triggerScenarios":"Using `expect(...).toMatchSnapshot()` outside of a running test (e.g. at module top level, in a worker, inside a plain script, in a setup hook that runs outside test context); calling snapshot assertions via a manually constructed `expect` that bypasses Vitest's test-aware wrapper.","commonSituations":"Calling `expect` inside module-level code or `globalThis` callbacks; using `expect` from a different package than the test runner; extracting `expect` into a helper that runs outside the test's async context; running assertion code in a detached worker.","solutions":["Move the `expect(...).toMatchSnapshot()` call inside an `it`/`test` body so the test context is active.","If inside an async callback, ensure it is awaited within the test so the context is preserved.","Import `expect` from `vitest` (not a standalone copy) so the test-aware instance is used."],"exampleFix":"// before (module scope)\nexpect(value).toMatchSnapshot()\n\n// after\ntest('snapshots value', () => {\n  expect(value).toMatchSnapshot()\n})","handlingStrategy":"validation","validationCode":"import { getWorkerState } from 'vitest'\nfunction inTestContext() {\n  return !!getWorkerState()?.currentTask\n}\nif (!inTestContext()) throw new Error('snapshot must run inside a test')\nexpect(value).toMatchSnapshot()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep expect(...).toMatchSnapshot() inside an it/test body.","Import expect from vitest so the test-aware instance is used.","Avoid module-level or detached-worker snapshot calls."],"tags":["snapshot","test-context","api-misuse","chai"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}