API Reference
Exported functions, constants, and utilities
defineWorkspaceConfig
Creates a Vitest workspace configuration by merging defaultWorkspaceConfig with your overrides.
import { defineWorkspaceConfig } from "@octopus-energy/vitest";defineWorkspaceConfigProp
Type
import { defineWorkspaceConfig } from "@octopus-energy/vitest";
export default defineWorkspaceConfig({
test: {
projects: ["packages/*"],
},
});defineProjectConfig
Creates a Vitest project configuration by merging both
defaultWorkspaceConfig and
defaultProjectConfig with your overrides.
import { defineProjectConfig } from "@octopus-energy/vitest";defineProjectConfigProp
Type
import { defineProjectConfig } from "@octopus-energy/vitest";
export default defineProjectConfig({
test: {
environment: "happy-dom",
setupFiles: ["./test/setup.ts"],
},
});defaultWorkspaceConfig
The raw default workspace-level configuration constant. Exposed for consumers who need manual merging control.
import { defaultWorkspaceConfig } from "@octopus-energy/vitest";defaultWorkspaceConfigProp
Type
Defaults
| Option | Value |
|---|---|
coverage.enabled | false |
coverage.exclude | ["**/node_modules/**", "**/dist/**", "**/*.config.*", "**/test/**", "**/*.spec.ts", "**/*.test.ts"] |
coverage.provider | "v8" |
coverage.reporter | ["text", "json-summary", "json"] |
coverage.reportsDirectory | "./coverage" |
Coverage is disabled by default for fast local development. Enable it with the --coverage flag:
npm exec vitest run --coverageYou can override any coverage option:
import { defineWorkspaceConfig } from "@octopus-energy/vitest";
export default defineWorkspaceConfig({
test: {
coverage: {
enabled: true,
reporter: ["lcov", "text"],
},
},
});Most projects should use defineWorkspaceConfig instead, which handles
merging automatically.
defaultProjectConfig
The raw default project-level configuration constant. Exposed for manual merging.
import { defaultProjectConfig } from "@octopus-energy/vitest";defaultProjectConfigProp
Type
Defaults
| Option | Value | Effect |
|---|---|---|
environment | "node" | Default for library packages |
globals | true | Enables describe, test, expect without imports |
mockReset | true | Resets mock implementations and clears call history |
restoreMocks | true | Restores original non-mocked implementations |
unstubEnvs | true | Restores process.env after each test |
unstubGlobals | true | Restores global variables after each test |
You can override any project default:
import { defineProjectConfig } from "@octopus-energy/vitest";
export default defineProjectConfig({
test: {
environment: "happy-dom",
setupFiles: ["./test/setup.ts"],
},
});When using defaultProjectConfig directly, workspace-level settings like coverage are not
included. Prefer defineProjectConfig which automatically merges both
workspace and project defaults.
MSW fixture
A configurable MSW fixture helper for Vitest's test.extend. Each test that requests
msw gets a fresh server with automatic setup and cleanup. The fixture calls
resetHandlers() and restoreHandlers() for each instance to reset handler state.
Unhandled network traffic and handler exceptions fail the test by default.
See the MSW fixture guide for setup, usage, and policies. Choose the entrypoint matching your installed MSW major version.
import { withMsw, type MswOptions, type MswFixture } from "@octopus-energy/vitest/msw/v2";withMswProp
Type
MswOptionsProp
Type
MSW turns HTTP handler exceptions into HTTP 500 responses. By default, the fixture fails the test
even if it accepts that response. failOnUnhandledException: false disables only the fixture's
exception assertion. Return explicit error responses from handlers when testing HTTP error paths.
fromPartial
A type-safe partial mock helper for use inside test files. The function is a runtime no-op that simply casts the input. The real value is in the type system: it infers the type from the calling context rather than from the partial object itself.
See the testing utilities guide for contextual typing and mocked function examples.
import { fromPartial } from "@octopus-energy/vitest/utils";fromPartialProp
Type