Skip to content

test: migrate unit test runner from jest to vitest#656

Merged
gewenyu99 merged 1 commit into
yara-improvementsfrom
posthog-code/warlock-vitest
Jun 15, 2026
Merged

test: migrate unit test runner from jest to vitest#656
gewenyu99 merged 1 commit into
yara-improvementsfrom
posthog-code/warlock-vitest

Conversation

@gewenyu99

@gewenyu99 gewenyu99 commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Problem

Closes #519

The wizard's source is ESM ("type": "module"), but the unit suite still ran on jest, whose ESM support is opt-in and finicky. That friction is visible in the warlock work this stacks on: the __mocks__/@posthog/warlock.ts drift guard, babel-jest for nanostores, and the .js.ts moduleNameMapper all exist to work around jest. Sibling repos (warlock, context-mill) already use vitest.

Stacked on #597 (yara-improvements) — this is that PR "made vite". Review/merge #597 first.

Changes

  • vitest.config.tsresolve.alias mirrors the old jest moduleNameMapper (manual mocks for @anthropic-ai/claude-agent-sdk, @posthog/warlock, ink + the tsconfig path aliases); a small pre-resolve plugin maps NodeNext .js import specifiers to their .ts source; globals: true, node env, v8 coverage.
  • Mechanical codemod across all test/mock files — jest.fn/mock/spyOn/useFakeTimers/...vi.*, and the jest.Mock/Mocked/MockedFunction/... ambient types → vitest equivalents (bridged as globals in types/vitest-global-types.d.ts so call sites stay bare).
  • Non-mechanical portsrequireMock → mocked imports, isolateModulesresetModules + dynamic import, hoisted-factory vars → vi.hoisted, and partial mocks that omit an accessed export → importOriginal (vitest throws where jest silently returned undefined, e.g. RunPhase/WizardError).
  • CLI re-import tests — the dispatch fires detached async work that ran synchronously under jest's babel-commonjs imports; settle() now anchors on the decisive sink before asserting, and env setup mutates process.env in place (reassigning it defeats yargs' .env() reader once the module graph is reset).
  • e2e left on jest — the standalone e2e-tests package (own jest.config.ts + ts-jest) keeps jest; it now has a self-contained tsconfig and is dropped from the root tsconfig/eslint scope. jest + its babel deps stay installed for that suite (the pnpm trust policy blocks pruning them cleanly), so the dependency diff is additive: +vitest, +@vitest/coverage-v8.

The warlock manual mock stays (per #519's note): the real package loads a YARA-X WASM binary at runtime that the unit runner still can't execute — resolve.alias replaces jest's moduleNameMapper to wire it, and its compile-time drift guard is unchanged.

Test plan

  • pnpm test — 737/737 pass (build → postbuild smoke-test → vitest run), verified stable across repeated full-suite runs.
  • pnpm typecheck and pnpm lint — clean (0 errors).

Created with PostHog Code from a Slack thread

Swaps the unit suite's runner from jest to vitest, removing jest's finicky
ESM workarounds now that the wizard's source is ESM-native.

- Add vitest.config.ts: resolve.alias mirrors the old jest moduleNameMapper
  (manual mocks + tsconfig path aliases), a small pre-resolve plugin maps
  NodeNext `.js` import specifiers back to their `.ts` source, globals + node
  environment, and v8 coverage.
- Codemod every test/mock file: jest.fn/mock/spyOn/timers → vi.*, and the
  jest.Mock/Mocked/... ambient types → vitest equivalents (bridged globally in
  types/vitest-global-types.d.ts so call sites stay bare).
- Port the APIs without a 1:1 vitest mapping: requireMock → mocked imports,
  isolateModules → resetModules + dynamic import, hoisted-factory vars →
  vi.hoisted, and partial module mocks that omit an accessed export →
  importOriginal (vitest throws where jest returned undefined).
- Stabilise the CLI re-import tests: the dispatch fires detached async work
  that used to run synchronously under jest's babel-commonjs imports; settle()
  now anchors on the decisive sink before asserting, and env-var setup mutates
  process.env in place (reassigning it defeats yargs' .env() reader once the
  module graph is reset).
- Keep jest for the standalone e2e-tests package (own jest.config + ts-jest):
  give it a self-contained tsconfig and drop it from the root tsconfig/eslint
  scope. jest and its babel deps stay installed for that suite.

737/737 unit tests pass; `pnpm build`, `pnpm typecheck`, and `pnpm lint` are
clean; the postbuild smoke-test still runs via `pnpm test`.

Generated-By: PostHog Code
Task-Id: 4ced191a-7fea-49ed-a854-ab6330f1c83d
@github-actions

Copy link
Copy Markdown

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci basic-integration
  • /wizard-ci error-tracking-upload-source-maps
  • /wizard-ci misc
  • /wizard-ci revenue

Test an individual app:

  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
Show more apps
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci error-tracking-upload-source-maps/android
  • /wizard-ci error-tracking-upload-source-maps/flutter
  • /wizard-ci error-tracking-upload-source-maps/ios
  • /wizard-ci error-tracking-upload-source-maps/next
  • /wizard-ci error-tracking-upload-source-maps/next-no-posthog
  • /wizard-ci error-tracking-upload-source-maps/node-raw
  • /wizard-ci error-tracking-upload-source-maps/node-rollup
  • /wizard-ci error-tracking-upload-source-maps/node-rollup-typescript-plugin
  • /wizard-ci error-tracking-upload-source-maps/node-webpack
  • /wizard-ci error-tracking-upload-source-maps/nuxt-3-6
  • /wizard-ci error-tracking-upload-source-maps/nuxt-4-3
  • /wizard-ci error-tracking-upload-source-maps/react-native
  • /wizard-ci error-tracking-upload-source-maps/react-vite
  • /wizard-ci error-tracking-upload-source-maps/rust
  • /wizard-ci misc/quack-quack
  • /wizard-ci revenue/stripe

Results will be posted here when complete.

@gewenyu99 gewenyu99 marked this pull request as ready for review June 15, 2026 14:24
@gewenyu99 gewenyu99 requested a review from a team June 15, 2026 14:24

@sarahxsanders sarahxsanders left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran all the tests locally a bunch of times to ensure no flakiness, looks like we are good. nothing jumping out to me as a blocker

@gewenyu99 gewenyu99 merged commit 32ba486 into yara-improvements Jun 15, 2026
15 checks passed
@gewenyu99 gewenyu99 deleted the posthog-code/warlock-vitest branch June 15, 2026 21:25
sarahxsanders added a commit that referenced this pull request Jun 16, 2026
Resolve conflicts from main's progress against this branch's jest->vitest
migration (#656). Keep vitest mechanics, fold in main's newer test content:

- package.json: keep `test:watch: vitest`, add main's `screens:check` script
- analytics.test.ts: keep vi.fn() mocks, add main's `identify` mock
- health-checks.test.ts: keep `as Mock`, take main's renamed no-connection tests
- claude-web.test.ts: adopt main's openTrackedLink mock in vitest syntax

Also convert jest-style test files main added after the migration so they run
under vitest (vi.fn/vi.mock, vi.hoisted for backup mocks) and fix `as jest.Mock`
type casts to `as Mock` so typecheck passes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants