Skip to content

Spec: replace Jest and Babel with Vitest and convert the tests to TypeScript #295

Closed
12 of 12 issues completed
Closed

Description

@jtklein-agents

Written from the map #285. Each decision below links to the ticket that settled it.

Problem Statement

The shell builds the app with Vite, but the tests run on a second toolchain: Jest, with a Babel transform that exists only for Jest. A developer keeps two transforms in step, carries a resolutions pin and a set of @babel/* packages that nothing else uses, and waits about 41 seconds of summed file time for 66 test files. The tests are also the last JavaScript the typecheck does not hold to the standard of the rest of src: they are .js, most carry @ts-nocheck, and 38 of them assign an untyped object to the renderer bridge, so a test can stub a bridge method that no longer exists and still pass.

Solution

The tests run under Vitest, on the Vite the shell already uses. Jest, babel-jest, every direct @babel/* entry and the Babel transform are gone. Every test file and test support file is TypeScript, held to the same bar as other TypeScript under src. yarn react-test and CI run Vitest, and the same 471 tests pass.

User Stories

  1. As a developer, I want the tests to run on the same Vite toolchain as the shell, so that I keep one transform instead of two.
  2. As a developer, I want yarn react-test to keep its name and run Vitest, so that my habits and CI do not change.
  3. As a developer, I want the same 471 tests in the same 66 files to pass after the swap, so that I know the swap lost no coverage.
  4. As a developer, I want the suite to finish faster, so that I run it more often.
  5. As a developer, I want no jest, babel-jest or direct @babel/* entry in package.json, so that I have fewer dependencies to update.
  6. As a developer, I want the Babel resolutions pin removed, so that no stale pin shapes the lockfile.
  7. As a developer, I want the removed packages gone from yarn.lock as well, so that the lockfile matches the manifest.
  8. As a developer, I want only vitest and jsdom added, so that the swap brings in no package nobody decided on.
  9. As a developer, I want one Vitest config with one environment and one setup list, so that every test runs the same way.
  10. As a developer, I want the test run to print no config warning, so that real warnings stand out.
  11. As a developer, I want every test to import test, expect and vi from vitest, so that I can see where each name comes from.
  12. As a developer, I want Vitest globals off, so that vi and expect are not in scope in production source.
  13. As a developer, I want a stray jest. or an unimported expect to fail the typecheck, so that CI catches a half-migrated test.
  14. As a developer, I want the test support files in one directory under src, so that the typecheck covers them like other source.
  15. As a developer, I want no jest/ directory left, so that nothing suggests Jest is still in use.
  16. As a developer, I want the default renderer bridge still installed before the page store loads, so that store tests keep importing Run at the top of the file.
  17. As a developer, I want the MobX action-warning check still applied to every test, so that an action outside a MobX action still fails a test.
  18. As a developer, I want the tests that reload a module to keep proving the same import-time behaviour, so that the swap does not weaken them.
  19. As a developer, I want the test that mocks a module to keep mocking it under Vitest, so that it still tests the unit alone.
  20. As a developer, I want the tests that render JSX to load under Vite, so that the page paint test still runs.
  21. As a developer, I want yarn react-test to still fail on an empty page paint, so that the guard stays.
  22. As a developer, I want the setImmediate polyfill removed from the one test that had it, so that no dead workaround stays.
  23. As a developer, I want yarn lint to pass with the Jest lint preset removed, so that lint does not depend on Jest.
  24. As a developer, I want the disable comments for the removed lint rules deleted, so that lint does not report unknown rules.
  25. As a developer, I want every test file to be .ts or .tsx, so that the editor and tsc check my tests.
  26. As a developer, I want converted tests to have zero diagnostics under --noImplicitAny, so that tests meet the bar of other TypeScript under src.
  27. As a developer, I want nothing added to the typecheck baseline, so that the conversion adds no debt.
  28. As a developer, I want no @ts-nocheck or @ts-ignore in a test, so that no test is silently unchecked.
  29. As a developer, I want @ts-expect-error allowed only with a one-line reason where a test passes a wrong type on purpose, so that runtime guards stay testable.
  30. As a developer, I want one helper that installs a test bridge from a Partial<RendererBridge>, so that tsc checks each bridge method a test supplies.
  31. As a developer, I want a test that stubs a bridge method the renderer bridge no longer has to fail the typecheck, so that tests follow the bridge type.
  32. As a developer, I want stubBridge to go through that same helper, so that one place writes to the bridge.
  33. As a developer, I want mocks written as plain vi.fn(), with a type argument only where tsc asks for it, so that tests stay short.
  34. As a developer, I want a partial store stand-in cast where it is used, so that there are no shared fixture builders to learn.
  35. As a developer, I want the e2e helper tests to stay beside their helpers and be typechecked, so that they meet the same bar without moving.
  36. As a developer, I want the Playwright specs left alone, so that this change touches one runner.
  37. As a reviewer, I want the runner swap in one pull request with no test converted to TypeScript, so that I review the runner change alone.
  38. As a reviewer, I want each conversion ticket to hold at most about 1,200 lines of test code, so that I can review it in one sitting.
  39. As a reviewer, I want each conversion to be a pure rename commit and then a typing commit, with the tests passing after each, so that the typing diff is readable.
  40. As a reviewer, I want the same test count per file before and after each ticket, so that I can see no test was dropped.
  41. As an agent picking up a ticket, I want the swap to block every conversion ticket, so that I never write types against Jest.
  42. As an agent picking up a ticket, I want the support-files ticket to block the page and store conversions, so that the bridge helper exists before I need it.
  43. As an agent picking up a ticket, I want the main-process and shell-config conversions to depend only on the swap, so that they run in parallel with the support-files ticket.
  44. As an agent picking up a ticket, I want the store conversions to touch separate files, so that they run in parallel without conflicts.
  45. As an agent picking up a ticket, I want the local agent docs to describe Vitest once the swap merges, so that I do not follow Jest instructions.
  46. As the driving dev, I want the swap ticket to carry the agent-doc edits as a checklist, so that I can apply them in the main checkout, where those files are kept out of git.
  47. As the driving dev, I want a closing ticket that checks the done list, so that the spec ends with Jest and Babel provably gone.
  48. As a maintainer, I want there never to be two test runners at once, so that CI and the config never split the files between them.

Implementation Decisions

Toolchain (#286, #287)

  • Add vitest@^5.1.1 and jsdom@^29.1.1 as dev dependencies. jsdom 30 and later need a newer Node 22 than the repo pins. Any other new package needs its own decision. @vitejs/plugin-react is not needed in the test config: Vite reads the JSX runtime and class-field semantics from the TypeScript config.
  • The config is a root vitest.config.mjs (#292). Vitest cannot read the electron-vite config. The .mjs name avoids a Vite warning on every run.
  • One config for every test (#289): jsdom environment, one ordered setup list (bridge setup, then the MobX action-warning check), no projects and no per-file environment comments. globals is off. include covers the tests under src and the e2e helper tests.
  • The config carries the aliases Jest's module-name map carried (electron, the Sentry renderer entry) and the one filenamify alias from the renderer build. CSS needs no mapping.
  • clearMocks is set to false, to match Jest. The suite passes either way; the explicit value keeps behaviour unchanged.

The swap: one ticket, one cutover (#292)

  • All 66 test files move to Vitest while still JavaScript, and Jest and Babel are removed in the same ticket. There is no two-runner period.
  • jest.fn, jest.spyOn and jest.restoreAllMocks become the vi.* calls of the same name.
  • Every test and setup file imports the names it uses from vitest (#288). Each file keeps test or it, whichever it uses today.
  • Five module-loading rewrites, as proven on the spike branch 287-vitest-spike: the four files that use jest.isolateModules use vi.resetModules() and await import(), and their tests and loader helpers become async; the one file that uses jest.mock with require uses a top-level vi.mock with its mock functions in vi.hoisted, then await import().
  • The four test files that hold JSX are renamed to .jsx with no content change.
  • The support files move from jest/ to src/test/, still JavaScript, with their import paths updated (#290). The Babel transform, the file mock and the Jest config are deleted. No jest/ directory remains.
  • package.json: the test scripts run Vitest under the same script names; jest, babel-jest, every direct @babel/* entry and the Babel resolutions pin are removed, with their yarn.lock stanzas.
  • The react-app/jest lint preset is removed and nothing replaces it (#291). The five testing-library disable comments are deleted, because ESLint fails on a disable comment for a rule it does not know.
  • The setImmediate polyfill in the updates test is deleted. Vitest's jsdom keeps Node's setImmediate.
  • The swap ticket's body lists the edits to the local agent docs (the checks doc, AGENTS.md, CODING_STANDARDS.md) as a checklist. Those files are kept out of git on the driving dev's clone, so no pull request carries them. The driving dev applies the checklist in the main checkout when the swap merges.

Typed tests (#293)

  • One helper in src/test/, named installBridge, takes Partial<RendererBridge>, holds the only cast to the full type, and is the only code that writes to window.rendererBridge. stubBridge takes the same type and calls it. No test assigns to window.rendererBridge directly.
  • The helper keeps the existing rule: a test's bridge is merged onto the one default bridge object the setup installed. It does not replace that object, and it does not spread or read host.
  • Mocks are plain vi.fn(). A type argument is added only where a test reads the mock's calls or results and tsc asks for it.
  • A partial store stand-in is cast at the site. There are no shared fixture builders.
  • No @ts-nocheck and no @ts-ignore. @ts-expect-error with a one-line reason is allowed only where a test passes a wrong type on purpose to exercise a runtime guard.
  • These rules are added to CODING_STANDARDS.md through the swap ticket's doc checklist.

Conversion tickets (#294)

  • Each conversion follows the existing leaf rule: a rename commit (git mv, R100), then a typing commit, with the tests passing after each. Each ticket replaces the direct bridge assignments in its own files with installBridge.
  • Twelve tickets on the spec's feature branch. How the test files group into conversion tickets after the swap #294 lists the files of each store ticket.
# Ticket Blocked by
0 The swap none
1 Support files and installBridge 0
2 Main-process tests 0
3 Shell-config tests and e2e helper tests 0
4 Page tests outside the page store, with .jsx → .tsx 1
5 Page store: alignment 1
6 Page store: partition 1
7 Page store: run computeds, options and input actions 1
8 Page store: run lifecycle, run list, start and cancel 1
9 Page store: substitution, tree, model and input-file options 1
10 Page store: citation, host, menu and the small bridge-call tests 1
11 Closing ticket 1 to 10
  • Ticket 3 carries the typecheck wiring for the e2e helper tests: one include in the TypeScript config and a wider second pass in the typecheck-baseline script, so those four files meet the --noImplicitAny bar. The four tests stay beside their CommonJS helpers and load them with a plain import.
  • The closing ticket narrows the Vitest include to .ts and .tsx, fixes the comment in the Playwright config that says Jest owns *.test.js, and checks the done list below.

Done list

  • Every file Jest runs today (62 under src, 4 e2e helper tests) and the support files are TypeScript and run under Vitest, with the same tests passing.
  • package.json has no jest, babel-jest or direct @babel/* entry, including the resolutions pin, and the Babel transform is gone.
  • Converted tests have zero diagnostics under --noImplicitAny, and nothing is added to the typecheck baseline.
  • CI's yarn react-test runs Vitest.

Testing Decisions

  • The seam is the existing one: the test suite, run by yarn react-test, as CI runs it. This spec adds no seam and changes no production behaviour, so the suite is its own regression test.
  • A good check here compares outcomes, not internals: the same 66 files and 471 tests pass, with the same test count per file as under Jest on master. Each ticket's pull request states the count before and after.
  • The one new module with logic is installBridge. It gets a small test of its external behaviour: after the call, the page sees the supplied methods on the same default bridge object, and the defaults the setup stubs are still there. Prior art: the existing proof tests for the support files (the stubBridge test and the MobX action-warning proof test).
  • Type behaviour is checked by yarn typecheck:baseline, not by runtime tests: the second pass fails on any diagnostic in a .ts or .tsx file, and an unimported expect or a stray jest. is an undeclared name once Jest's types are gone.
  • Every ticket passes yarn lint, yarn typecheck:baseline and yarn react-test, the three checks CI runs.
  • The swap ticket also proves the removed package names are absent from both package.json and yarn.lock.
  • Prior art for the conversions: the earlier JS→TS leaves on the page and the main process, which used the same rename-then-type commits.

Out of Scope

  • Replacing eslint-config-react-app and the Babel it pulls in transitively. Vitest lint rules go with that effort.
  • Converting the Playwright specs, the Playwright configs and the non-test e2e helpers. They belong to a different runner.
  • A Node environment for the tests that need no DOM.
  • Adding, removing or rewriting tests beyond what the runner and the types require.
  • Changes to production source. If a typed test exposes a wrong production type, that fix is its own ticket.

Further Notes

  • The spike branch 287-vitest-spike holds a working config and the nine file fixes. It is throwaway and is never merged; use it as a reference for the swap. It kept ambient globals and a jest shim, which the swap must not keep.
  • Known loss from removing the lint preset: jest/valid-expect, jest/valid-expect-in-promise, jest/no-conditional-expect and jest/no-identical-title. TypeScript and Vitest's own failure on an unawaited async assertion cover part of that.
  • Line counts and file lists in How the test files group into conversion tickets after the swap #294 are from master at 2044cbc. A ticket checks its file list against the tree before it starts.
  • The spike measured summed file time falling from about 41 s to about 14 s, and 43 packages added to yarn.lock before the Jest and Babel removals.

Sub-issues

Sub-issues

12 of 12 Issues completed
Loading 12 sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    ready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions