Skip to content

chore(core): run nx package unit tests with vitest - #36754

Draft
FrozenPandaz wants to merge 18 commits into
masterfrom
worktree-vitest-nx-explore
Draft

chore(core): run nx package unit tests with vitest#36754
FrozenPandaz wants to merge 18 commits into
masterfrom
worktree-vitest-nx-explore

Conversation

@FrozenPandaz

Copy link
Copy Markdown
Contributor

Current Behavior

The nx package's ~6400 unit tests run with Jest, pinned to maxWorkers: 1 (a full nx test nx takes ~4 minutes), on top of a custom resolver, an SWC mut-cjs-exports plugin, and half a dozen CJS mock shims for ESM-only dependencies.

Expected Behavior

nx test nx runs the same 6423 tests with Vitest 4, inferred through the @nx/vitest plugin. The suite is parallel-safe (verified over repeated parallel and serial runs), which brings the wall time to ~50s (~4.5x faster). Other projects are unaffected and still infer Jest.

Highlights:

  • vitest.config.mts reproduces the Jest setup's special behavior: the @nx/nx-source resolve condition (replacing jest-resolver.js), a plugin routing the napi loader to the self-contained native-bindings.js, deep nx/src/* import aliases, and the CJS yargs entry.
  • vitest.setup.mts ports the unit-test-setup.js guards to vi.doMock, registers @swc-node/register so the codebase's lazy require() calls can load TS source (restoring Error.prepareStackTrace afterwards — the hook's source-map-support otherwise mis-maps vite-transformed frames, breaking error locations and inline-snapshot updates), and pins color detection off so snapshots are stable regardless of how the suite is invoked.
  • A new internal-testing-utils/cjs-mock.ts helper patches Module._load jest-style so specs can mock modules that the source loads with bare require() — the channel vi.mock cannot reach.
  • Spec migration was largely codemodded: jest.*vi.* (with real hoisting), jest.requireActualawait vi.importActual, jest.isolateModulesvi.resetModules() + dynamic import(), done() callbacks → promises, plus per-file fixes for Vitest semantics (constructible class mocks, spy-mode module mocks for frozen ESM namespaces, hooks that must not return mocks, vi.resetAllMocks restoring real spy implementations).
  • Snapshots regenerated for Vitest's serializer; the ESM mock shims for @clack/prompts/ora/chalk/etc. are no longer needed for this suite.

The migration also surfaced and fixed two latent test bugs where assertions passed while the mock never applied — one of which let a spec overwrite the repository's real nx.json during test runs.

jest.config.cts and jest-resolver.js are removed; the nx-scoped branches in scripts/unit-test-setup.js are now dead code and can be cleaned up separately.

Related Issue(s)

N/A


View Polygraph session ↗

@netlify

netlify Bot commented Aug 21, 2026

Copy link
Copy Markdown

Deploy Preview for nx-dev ready!

Name Link
🔨 Latest commit c9b0bee
🔍 Latest deploy log https://app.netlify.com/projects/nx-dev/deploys/6a88f09e400e890008c5e627
😎 Deploy Preview https://deploy-preview-36754--nx-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Aug 21, 2026

Copy link
Copy Markdown

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit c9b0bee
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/6a88f09e5d156800087b3d33
😎 Deploy Preview https://deploy-preview-36754--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@FrozenPandaz
FrozenPandaz force-pushed the worktree-vitest-nx-explore branch from 03bd6c7 to 898b15c Compare August 21, 2026 19:38
@nx-cloud

nx-cloud Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit c9b0bee

Command Status Duration Result
nx affected --targets=lint,oxlint,test,build,e2... ✅ Succeeded 37m 36s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 3s View ↗
nx-cloud record -- pnpm nx-cloud conformance:check ✅ Succeeded 57s View ↗
nx build workspace-plugin ✅ Succeeded <1s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 21s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded 20s View ↗

☁️ Nx Cloud last updated this comment at 2026-08-22 01:25:04 UTC

@nx-cloud nx-cloud Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ The fix from Nx Cloud was applied

We guard mock-project-graph.ts, mock-prettier.ts, and mock-fs.ts with typeof vi !== 'undefined' so they fall back to the equivalent jest.* APIs when consumed by packages that still run under Jest, fixing the ReferenceError: vi is not defined failures across ~20 packages. We also remove the stray it.only left in store-run-information-life-cycle.spec.ts, which caused Vitest to abort nx:test with an unexpected .only modifier error.

Tip

We verified this fix by re-running @nx/oxlint:test, rsbuild:test, playwright:test and 5 more.

Warning

The suggested diff is too large to display here, but you can view it on Nx Cloud ↗


🔔 Heads up, your workspace has pending recommendations ↗ to auto-apply fixes for similar failures.

Revert fix via Nx Cloud  

View interactive diff ↗

➡️ This fix was applied by Jason Jean

🎓 Learn more about Self-Healing CI on nx.dev

@FrozenPandaz
FrozenPandaz force-pushed the worktree-vitest-nx-explore branch from 207f512 to 5af2b9c Compare August 21, 2026 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant