Title: unit-test (vitest): isolate:false default silently breaks cross-file vi.mock() of shared modules on low-CPU workers · Issue #33570 · angular/angular-cli · GitHub
Open Graph Title: unit-test (vitest): isolate:false default silently breaks cross-file vi.mock() of shared modules on low-CPU workers · Issue #33570 · angular/angular-cli
X Title: unit-test (vitest): isolate:false default silently breaks cross-file vi.mock() of shared modules on low-CPU workers · Issue #33570 · angular/angular-cli
Description: Which @angular/* package(s) are the source of the bug? build Is this a regression? No Description @angular/build:unit-test (Vitest runner) sets test.isolate: false by default (to match the historical Karma/Jasmine experience — src/builde...
Open Graph Description: Which @angular/* package(s) are the source of the bug? build Is this a regression? No Description @angular/build:unit-test (Vitest runner) sets test.isolate: false by default (to match the historic...
X Description: Which @angular/* package(s) are the source of the bug? build Is this a regression? No Description @angular/build:unit-test (Vitest runner) sets test.isolate: false by default (to match the historic...
Opengraph URL: https://github.com/angular/angular-cli/issues/33570
X: @github
Domain: Github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"unit-test (vitest): isolate:false default silently breaks cross-file vi.mock() of shared modules on low-CPU workers","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\nbuild\n\n### Is this a regression?\n\nNo\n\n### Description\n\n`@angular/build:unit-test` (Vitest runner) sets **`test.isolate: false`** by default (to match the historical Karma/Jasmine experience — `src/builders/unit-test/runners/vitest/plugins.js`, and again in `executor.js`). Combined with the builder's pre-bundled test entry points that **share esbuild chunks**, this silently breaks `vi.mock()` of a bare (node_modules) module across spec files that land on the same worker.\n\n**Mechanism**\n\n1. Each spec is pre-bundled by esbuild. A component/service imported by more than one spec is hoisted into a **shared chunk** (`chunk-*.js`), and third-party packages are kept external (`externalPackages: true`).\n2. With `isolate: false`, Vitest **reuses a worker (and its module registry) across spec files**.\n3. Spec **A** and spec **B** both `vi.mock('some-pkg', factory)` (different factories) and both import the same shared component that imports `some-pkg`. Whichever spec runs **first** on a worker instantiates the shared chunk bound to **its** mock. When the other spec later runs on the same worker, the cached shared-chunk module is reused, so **its own `vi.mock('some-pkg')` never intercepts the component's import** — its mock handles stay empty while the component silently runs against the *first* spec's mock (or the real module).\n\nBecause Vitest's worker count follows `os.availableParallelism()`, the failure is **CPU-count dependent**: green on high-core dev machines / CI, but **deterministically red on low-core CI runners** (e.g. a 2-vCPU Kubernetes/ARC pod), where many spec files share each worker in a stable, size-ordered sequence. This makes it a nasty \"works on my machine / fails only on constrained CI\" trap.\n\n### Minimal reproduction\n\nConditions (no third-party lib needed — any bare module works):\n\n- `pkg` — a trivial node_modules package exporting a factory function, e.g. `makeThing()`.\n- `SharedCmp` — a standalone component that imports `pkg` and calls `makeThing()` in `ngAfterViewInit`.\n- `a.spec.ts` — `vi.mock('pkg', () =\u003e ({ makeThing: () =\u003e handlesA }))`, imports `SharedCmp` (e.g. via a page that renders it), asserts against `handlesA`.\n- `b.spec.ts` — `vi.mock('pkg', () =\u003e ({ makeThing: () =\u003e handlesB }))`, creates `SharedCmp` directly, asserts against `handlesB`.\n\nRun `ng test` with the two specs while constraining workers so both files share one worker (e.g. on a 2-CPU machine, or `VITEST_MAX_THREADS=1`), with `a.spec.ts` scheduled before `b.spec.ts`. `b.spec.ts`'s assertions fail because `SharedCmp` is bound to `handlesA` (or the real module), and `handlesB` is never called. Adding a project `vitest-base.config.ts` with `test: { isolate: true }` (via `\"runnerConfig\": true`) makes both pass.\n\n### Exception or Error\n\n```\nNo thrown error — the component runs against the wrong (foreign) mock instance.\nSymptom: the later spec's own mock handles are never invoked\n(e.g. \"expected 1, got 0\" / \"undefined is not a spy\"), only on low-CPU workers.\n```\n\n### Your Environment\n\n```\n@angular/build: 21.2.12\nvitest: 4.1.7\nNode: 24.15.0\nRunner: vitest (unit-test builder), jsdom environment\nRepro trigger: low worker count (2-vCPU CI runner) — deterministic;\nhigh-core machines mask it.\n```\n\n### Anything else relevant?\n\n**Workaround (works):** opt into an external runner config and re-enable isolation —\n\n`angular.json`:\n```json\n\"test\": {\n \"builder\": \"@angular/build:unit-test\",\n \"options\": { \"runnerConfig\": true }\n}\n```\n`vitest-base.config.ts`:\n```ts\nimport { defineConfig } from 'vitest/config';\nexport default defineConfig({ test: { isolate: true } });\n```\n\n**Suggested fixes for consideration:**\n- Document prominently that `vi.mock()` of shared/bare modules is unsafe under the default `isolate: false` (the current in-repo guard only rejects *relative* `vi.mock` paths — `angular:vitest-mock-patch` — but bare-specifier mocks are the ones that break cross-file).\n- Consider detecting `vi.mock` usage and warning (or auto-isolating) when specs share chunks, or make isolation the default for the Vitest runner.\n\nHappy to package a StackBlitz/minimal repo if that would help triage.\n","author":{"url":"https://github.com/rommelandrea","@type":"Person","name":"rommelandrea"},"datePublished":"2026-07-15T06:10:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/33570/angular-cli/issues/33570"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:21686537-955a-d8f2-5509-4e16af87f548 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8298:E0378:54CFBA:747524:6A6274AB |
| html-safe-nonce | 417de73a8cc8b0add041edd74dc23385dceec07f0651424f899304207afab958 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Mjk4OkUwMzc4OjU0Q0ZCQTo3NDc1MjQ6NkE2Mjc0QUIiLCJ2aXNpdG9yX2lkIjoiNTMzNDAyMTc3NjE5NjQwMDI5OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 35e3ab63261f88024f654624228be6dea67881eb0ac9104edee1f29b7545d431 |
| hovercard-subject-tag | issue:4889826245 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular-cli/33570/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9d9923e74530bc347018d0f8ac2e3310e31be55fcedac08797f00f510d9d4d31/angular/angular-cli/issues/33570 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9d9923e74530bc347018d0f8ac2e3310e31be55fcedac08797f00f510d9d4d31/angular/angular-cli/issues/33570 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? build Is this a regression? No Description @angular/build:unit-test (Vitest runner) sets test.isolate: false by default (to match the historic... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rommelandrea |
| hostname | github.com |
| expected-hostname | github.com |
| None | 838e87bd0e4f5415707a930bc14e89e382dc2ea046e50ff575252eb2f2cb06a2 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular-cli git https://github.com/angular/angular-cli.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 36891867 |
| octolytics-dimension-repository_nwo | angular/angular-cli |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 36891867 |
| octolytics-dimension-repository_network_root_nwo | angular/angular-cli |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f0081192f88523d2bf4cfdf22bb3e138affc4cd1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width