chore(core): run nx package unit tests with vitest - #36754
Conversation
✅ Deploy Preview for nx-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for nx-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
03bd6c7 to
898b15c
Compare
|
View your CI Pipeline Execution ↗ for commit c9b0bee
☁️ Nx Cloud last updated this comment at |
There was a problem hiding this comment.
✅ 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.
View interactive diff ↗➡️ This fix was applied by Jason Jean
🎓 Learn more about Self-Healing CI on nx.dev
…ow-test timeout for vitest
…un-information spec
207f512 to
5af2b9c
Compare
Current Behavior
The
nxpackage's ~6400 unit tests run with Jest, pinned tomaxWorkers: 1(a fullnx test nxtakes ~4 minutes), on top of a custom resolver, an SWCmut-cjs-exportsplugin, and half a dozen CJS mock shims for ESM-only dependencies.Expected Behavior
nx test nxruns the same 6423 tests with Vitest 4, inferred through the@nx/vitestplugin. 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.mtsreproduces the Jest setup's special behavior: the@nx/nx-sourceresolve condition (replacingjest-resolver.js), a plugin routing the napi loader to the self-containednative-bindings.js, deepnx/src/*import aliases, and the CJSyargsentry.vitest.setup.mtsports theunit-test-setup.jsguards tovi.doMock, registers@swc-node/registerso the codebase's lazyrequire()calls can load TS source (restoringError.prepareStackTraceafterwards — 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.internal-testing-utils/cjs-mock.tshelper patchesModule._loadjest-style so specs can mock modules that the source loads with barerequire()— the channelvi.mockcannot reach.jest.*→vi.*(with real hoisting),jest.requireActual→await vi.importActual,jest.isolateModules→vi.resetModules()+ dynamicimport(),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.resetAllMocksrestoring real spy implementations).@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.jsonduring test runs.jest.config.ctsandjest-resolver.jsare removed; thenx-scoped branches inscripts/unit-test-setup.jsare now dead code and can be cleaned up separately.Related Issue(s)
N/A
View Polygraph session ↗