vitest-dev/vitest · error · Error

Snapshot environment module must have a default export…

Error message

Snapshot environment module must have a default export object with a shape of `SnapshotEnvironment`

What it means

Thrown by `resolveSnapshotEnvironment` when the module pointed to by `config.snapshotEnvironment` was successfully imported but its `default` export is not a non-null object. Vitest expects `mod.default` to be an object implementing the `SnapshotEnvironment` interface (`getVersion`, `getHeader`, `resolvePath`, `resolveRawPath`, `saveSnapshotFile`, `readSnapshotFile`, `removeSnapshotFile`, optional `processStackTrace`). A missing/`null`/non-object default is treated as a configuration error.

Solutions

  1. Ensure the module has `export default { getVersion() {...}, getHeader() {...}, resolvePath, resolveRawPath, saveSnapshotFile, readSnapshotFile, removeSnapshotFile }`.
  2. If exporting a class, instantiate it: `export default new MySnapshotEnvironment()`.
  3. Verify the resolved path matches the file you edited (check `test.snapshotEnvironment` value and the import map).

Example fix

// before
export class MyEnv { /* methods */ }

// after
export class MyEnv { /* methods */ }
export default new MyEnv()
Defensive patterns

Strategy: validation

Validate before calling

// after importing the candidate module
const mod = await import(config.snapshotEnvironment)
if (typeof mod.default !== 'object' || !mod.default) {
  throw new Error('snapshotEnvironment module needs a conforming default export')
}
const required = ['getVersion','getHeader','resolvePath','resolveRawPath','saveSnapshotFile','readSnapshotFile','removeSnapshotFile']
for (const k of required) if (typeof mod.default[k] !== 'function') throw new Error(`missing ${k}`)

Type guard

function isSnapshotEnvironment(v): boolean {
  return !!v && typeof v === 'object'
    && ['getVersion','getHeader','resolvePath','resolveRawPath','saveSnapshotFile','readSnapshotFile','removeSnapshotFile']
      .every(k => typeof v[k] === 'function')
}

Prevention

When it happens

Trigger: Pointing `test.snapshotEnvironment` to a module that has no default export, exports a function as default (instead of an object), exports the class instance under a named export, or exports `null`/`undefined` as default.

Common situations: Writing a custom snapshot environment and forgetting `export default`; exporting the class but not instantiating it; named-only exports; bundler interop producing `{ default: undefined }`; pointing the path at a file that re-exports from another module incorrectly.

Related errors


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

Appendix: source

Thrown at packages/vitest/src/integrations/snapshot/environments/resolveSnapshotEnvironment.ts:16

import type { SnapshotEnvironment } from '@vitest/snapshot/environment'
import type { SerializedConfig } from '../../../runtime/config'
import type { TestModuleRunner } from '../../../runtime/moduleRunner/testModuleRunner'

export async function resolveSnapshotEnvironment(
  config: SerializedConfig,
  moduleRunner: TestModuleRunner,
): Promise<SnapshotEnvironment> {
  if (!config.snapshotEnvironment) {
    const { VitestNodeSnapshotEnvironment } = await import('./node')
    return new VitestNodeSnapshotEnvironment()
  }

  const mod = await moduleRunner.import(config.snapshotEnvironment)
  if (typeof mod.default !== 'object' || !mod.default) {
    throw new Error(
      'Snapshot environment module must have a default export object with a shape of `SnapshotEnvironment`',
    )
  }
  return mod.default
}

View on GitHub (pinned to 1fa9837ec2)