Skip to content

Migrate test suites from Jest to Vitest #45

Description

@nojibe

Motivation

The project runs two separate Jest setups and the ESM story is painful. Migrating to Vitest gives us native ESM/TS support (no ts-jest, no --experimental-vm-modules), faster runs, a Jest-compatible API (low-friction migration), and unified web + node configs via a single vitest.workspace.

This ticket blocks #33 — CI should run the Vitest suites, not Jest, so this migration should land first.

Current state

  • Web suite (jest.config.js): next/jest, jsdom, @testing-library/react, setup in _jest.setup.web.ts (mocks next/server, polyfills URLPattern). Ignores src/cli/ and src/lib/.
  • CLI suite (jest.config.cli.js): ts-jest, node env, maxWorkers: 4, coverage, matches src/cli/**/*.test.ts and src/lib/**/*.test.ts.
  • Setup: _jest.setup.web.ts, _jest.setup.cli.ts (global.jest = jest).
  • ~95 test files. Heaviest areas: src/point-functions/__tests__ (24), src/lib/__tests__ (14), src/cli/services/__tests__ (13).
  • Deps: jest, @jest/globals, @types/jest, jest-environment-jsdom, ts-jest.
  • Scripts: test:web, test:cli, update-snapshots; test runs both.

Scope / plan

  1. Add vitest, @vitest/coverage-v8, jsdom, @testing-library/jest-dom (or vitest-compatible matchers); remove jest, ts-jest, @jest/globals, @types/jest, jest-environment-jsdom.
  2. Create a vitest.workspace.ts with two projects:
    • web: environment: 'jsdom', setup file port of _jest.setup.web.ts, @/ alias → src/, includes the current web globs.
    • cli/node: environment: 'node', includes src/cli/** and src/lib/**, coverage (v8) → coverage/cli.
  3. Alias ^@/(.*)$src/$1 (via vite-tsconfig-paths or resolve.alias).
  4. Port setup files: replace jest.mock('next/server', …) with vi.mock; keep the URLPattern polyfill; drop the global.jest = jest shim.
  5. Codemod test files: jest.fn/mock/spyOnvi.*, @jest/globals imports → vitest. Enable globals: true to minimize churn, or add explicit imports.
  6. Verify snapshots migrate (Vitest uses a compatible format; regenerate with --update where needed).
  7. Confirm aws-sdk-client-mock/sinon and next/server mocking still work under Vitest.
  8. Update package.json scripts: test:webvitest run --project web, test:clivitest run --project cli, testvitest run, update-snapshotsvitest run --update; drop the --experimental-vm-modules flag.
  9. Update CONTRIBUTING.md/docs test commands and remove Jest config files.

Acceptance criteria

  • All existing tests pass under Vitest (both web and cli/node projects)
  • pnpm test, pnpm test:web, pnpm test:cli run via Vitest; no --experimental-vm-modules
  • Jest and ts-jest removed from package.json and lockfile
  • Coverage still produced for the CLI suite
  • Docs updated to reference Vitest
  • No net loss in test coverage vs. the Jest run

Notes

Keep the two-project split (web jsdom vs. node) — the CLI/lib tests should not load the jsdom/Next setup. next/jest's SWC transform is replaced by Vitest's esbuild/SWC; watch for any Next-specific transform behavior (e.g. CSS module or next/font imports) in component tests.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions