vitest-dev/vitest · error · Error

Raw snapshot is required

Error message

Raw snapshot is required

What it means

SnapshotClient.assertRaw requires options.rawSnapshot (a raw snapshot descriptor with a file and content). If absent, the method cannot perform a raw snapshot comparison and throws immediately, before touching filepath.

Example fix

// before
client.assertRaw({ filepath, name: 'raw' })
// after
client.assertRaw({ filepath, name: 'raw', rawSnapshot: { file: './snap.txt', content: null } })
Defensive patterns

Strategy: validation

Validate before calling

if (!options.rawSnapshot || typeof options.rawSnapshot !== 'object') {
  throw new Error('assertRaw requires a rawSnapshot descriptor { file, content }')
}
client.assertRaw(options)

Type guard

function isRawSnapshot(v: unknown): v is { file: string; content: string | null } { return !!v && typeof v === 'object' && typeof (v as any).file === 'string' }

Prevention

When it happens

Trigger: Calling assertRaw without a rawSnapshot option; passing a plain string instead of a { file, content } raw snapshot descriptor; invoking the raw API from a path that did not build the rawSnapshot object.

Common situations: Using the raw snapshot API (e.g. for non-serialized file content like images/text) without constructing the rawSnapshot descriptor; wrapper that drops rawSnapshot when forwarding options.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/7eee66f32aba52f7. Report an issue: GitHub.

Appendix: source

Thrown at packages/snapshot/src/client.ts:348

      received: stableResult.rendered,
      expectedSnapshot,
      matchResult,
      isInline,
      error,
      assertionName: options.assertionName,
    })

    return {
      pass,
      message: () => `Snapshot \`${key}\` mismatched`,
      actual: actual?.trim(),
      expected: expected?.trim(),
    }
  }

  async assertRaw(options: AssertOptions): Promise<void> {
    if (!options.rawSnapshot) {
      throw new Error('Raw snapshot is required')
    }

    const { filepath, rawSnapshot } = options

    if (rawSnapshot.content == null) {
      if (!filepath) {
        throw new Error('Snapshot cannot be used outside of test')
      }

      const snapshotState = this.getSnapshotState(filepath)

      // save the filepath, so it don't lose even if the await make it out-of-context
      options.filepath ||= filepath
      // resolve and read the raw snapshot file
      rawSnapshot.file = await snapshotState.environment.resolveRawPath(
        filepath,
        rawSnapshot.file,
      )

View on GitHub (pinned to 1fa9837ec2)