{"id":"aefbb1e1ea68cfe4","repo":"vitest-dev/vitest","slug":"snapshot-environment-module-must-have-a-default-ex","errorCode":null,"errorMessage":"Snapshot environment module must have a default export object with a shape of `SnapshotEnvironment`","messagePattern":"Snapshot environment module must have a default export object with a shape of `SnapshotEnvironment`","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/snapshot/environments/resolveSnapshotEnvironment.ts","lineNumber":16,"sourceCode":"import type { SnapshotEnvironment } from '@vitest/snapshot/environment'\nimport type { SerializedConfig } from '../../../runtime/config'\nimport type { TestModuleRunner } from '../../../runtime/moduleRunner/testModuleRunner'\n\nexport async function resolveSnapshotEnvironment(\n  config: SerializedConfig,\n  moduleRunner: TestModuleRunner,\n): Promise<SnapshotEnvironment> {\n  if (!config.snapshotEnvironment) {\n    const { VitestNodeSnapshotEnvironment } = await import('./node')\n    return new VitestNodeSnapshotEnvironment()\n  }\n\n  const mod = await moduleRunner.import(config.snapshotEnvironment)\n  if (typeof mod.default !== 'object' || !mod.default) {\n    throw new Error(\n      'Snapshot environment module must have a default export object with a shape of `SnapshotEnvironment`',\n    )\n  }\n  return mod.default\n}\n","sourceCodeStart":1,"sourceCodeEnd":22,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/integrations/snapshot/environments/resolveSnapshotEnvironment.ts#L1-L22","documentation":"When `snapshotEnvironment` is configured, Vitest imports that module via the test module runner and expects a default export that is an object conforming to the `SnapshotEnvironment` interface (methods like `readSnapshotFile`, `writeSnapshotFile`, `resolveRawPath`, etc.). If `mod.default` is not an object, the resolver throws because the snapshot subsystem cannot operate without a valid environment implementation.","triggerScenarios":"Setting `test.snapshotEnvironment: './my-snapshot-env.js'` where the module has no default export, exports a class/function as default, or exports the environment as a named export instead of default.","commonSituations":"Authoring a custom snapshot environment (e.g. for an in-memory or remote store) and forgetting the `default` export, or migrating a named-export module to the expected default-object shape.","solutions":["Ensure the module has `export default { readSnapshotFile, writeSnapshotFile, resolveRawPath, removeSnapshotFile, resolveTestFilePath, ... }`.","If your environment is a class instance, export `new MyEnv()` as the default.","Verify the path in `snapshotEnvironment` resolves to the intended file."],"exampleFix":"// before\nexport class MySnapshotEnv { readSnapshotFile() {} /* ... */ }\n// after\nclass MySnapshotEnv { readSnapshotFile() {} /* ... */ }\nexport default new MySnapshotEnv()","handlingStrategy":"type-guard","validationCode":"import * as mod from './my-snapshot-env.js'\nif (typeof mod.default !== 'object' || !mod.default) {\n  throw new Error('snapshot env must default-export an object implementing SnapshotEnvironment')\n}","typeGuard":"import type { SnapshotEnvironment } from 'vitest/snapshot'\nfunction isSnapshotEnv(v: unknown): v is SnapshotEnvironment {\n  return typeof v === 'object' && v !== null\n    && typeof (v as any).readSnapshotFile === 'function'\n    && typeof (v as any).writeSnapshotFile === 'function'\n}","tryCatchPattern":null,"preventionTips":["Default-export an object (or class instance) implementing all `SnapshotEnvironment` methods.","Use the `SnapshotEnvironment` type to get compile-time coverage."],"tags":["snapshot","config","custom-environment","loader"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}