Skip to Content
Octopus Energy Packages

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";
defineWorkspaceConfig

Prop

Type

vitest.config.ts
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";
defineProjectConfig

Prop

Type

packages/my-package/vitest.config.ts
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";
defaultWorkspaceConfig

Prop

Type

Defaults

OptionValue
coverage.enabledfalse
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 --coverage

You can override any coverage option:

vitest.config.ts
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";
defaultProjectConfig

Prop

Type

Defaults

OptionValueEffect
environment"node"Default for library packages
globalstrueEnables describe, test, expect without imports
mockResettrueResets mock implementations and clears call history
restoreMockstrueRestores original non-mocked implementations
unstubEnvstrueRestores process.env after each test
unstubGlobalstrueRestores global variables after each test

You can override any project default:

packages/my-package/vitest.config.ts
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";
withMsw

Prop

Type

MswOptions

Prop

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";
fromPartial

Prop

Type

On this page