{"record":{"id":"108672be86643374","repo":"vitest-dev/vitest","slug":"snapshot-cannot-be-used-outside-of-test","errorCode":null,"errorMessage":"Snapshot cannot be used outside of test","messagePattern":"Snapshot cannot be used outside of test","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/snapshot/src/client.ts","lineNumber":134,"sourceCode":"\n  match(options: AssertOptions): MatchResult {\n    const {\n      filepath,\n      name,\n      testId = name,\n      message,\n      isInline = false,\n      properties,\n      inlineSnapshot,\n      error,\n      errorMessage,\n      rawSnapshot,\n      assertionName,\n    } = options\n    let { received } = options\n\n    if (!filepath) {\n      throw new Error('Snapshot cannot be used outside of test')\n    }\n\n    const snapshotState = this.getSnapshotState(filepath)\n    const testName = [name, ...(message ? [message] : [])].join(' > ')\n\n    // Probe first so we can mark as checked even on early return\n    const expectedSnapshot = snapshotState.probeExpectedSnapshot({\n      testName,\n      testId,\n      isInline,\n      inlineSnapshot,\n    })\n\n    if (typeof properties === 'object') {\n      if (typeof received !== 'object' || !received) {\n        expectedSnapshot.markAsChecked()\n        throw new Error(\n          'Received value must be an object when the matcher has properties',","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/snapshot/src/client.ts#L116-L152","documentation":"SnapshotClient.match throws 'Snapshot cannot be used outside of test' when the filepath option is falsy. The filepath ties a snapshot assertion to a test file; without it the snapshot cannot be located or written, so the assertion is rejected before any state lookup.","triggerScenarios":"Calling snapshotClient.match with options.filepath omitted, empty, or undefined; invoking toMatchSnapshot-style logic outside a test context where filepath is not propagated.","commonSituations":"Using the snapshot API from a custom runner that does not set filepath; calling snapshot assertions in setup/global hooks; a wrapper that drops the filepath field; inline snapshot used in a script context.","solutions":[],"exampleFix":"// before\nclient.match({ name: 'test', received: value })\n// after\nclient.match({ filepath: '/abs/test.ts', name: 'test', received: value })","handlingStrategy":"validation","validationCode":"if (!filepath || typeof filepath !== 'string') {\n  throw new Error('filepath is required for snapshot assertions')\n}\nclient.match({ filepath, name, received })","typeGuard":"function hasFilepath(o: { filepath?: string }): o is { filepath: string } { return typeof o.filepath === 'string' && o.filepath.length > 0 }","tryCatchPattern":null,"preventionTips":["Thread filepath from the test file into every snapshot assertion","Avoid snapshot assertions in global/setup hooks","Wrap the client to assert filepath at the boundary"],"tags":["snapshot","lifecycle","filepath"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}