Title: Allow using `vi.mock` (and related) functions in tests · Issue #32641 · angular/angular-cli · GitHub
Open Graph Title: Allow using `vi.mock` (and related) functions in tests · Issue #32641 · angular/angular-cli
X Title: Allow using `vi.mock` (and related) functions in tests · Issue #32641 · angular/angular-cli
Description: Which @angular/* package(s) are relevant/related to the feature request? core Description With the new Angular unit-test builder (Vite), test files are bundled into unified chunks. Because of this full bundling step: ESM modules are stat...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? core Description With the new Angular unit-test builder (Vite), test files are bundled into unified chunks. Because of this ...
X Description: Which @angular/* package(s) are relevant/related to the feature request? core Description With the new Angular unit-test builder (Vite), test files are bundled into unified chunks. Because of this ...
Opengraph URL: https://github.com/angular/angular-cli/issues/32641
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow using `vi.mock` (and related) functions in tests","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\n\ncore\n\n### Description\n\nWith the new Angular unit-test builder (Vite), test files are bundled into unified chunks.\nBecause of this full bundling step:\n\n- ESM modules are statically linked at build time\n- There is no runtime module graph available\n- vi.mock() cannot intercept module loading\n- Mocking entire modules (components/services/modules) is not possible\n\nCurrently if we use `vi.mock` function for automocking component/directives/services/pipes/etc..., we got an error:\n\n```\nError: The \"vi.mock\" and related methods are not supported with the Angular unit-test system. Please use Angular TestBed for mocking.\n```\n\nTestBed overrides are insufficient because:\n\n- They replace Angular metadata (providers/imports), not module implementation\n- They cannot replace pure TS logic or side effects\n- They do not intercept ESM import bindings\n\n### Proposed solution\n\nInstead of runtime interception (like Vitest normally does), Angular test builder should:\n\n1. Detect `vi.mock()` calls at compile time (in spec files, and in setupFiles)\n2. Hoist them\n3. Rewrite import bindings to use a generated mock registry\n4. Replace module resolution during bundling\n\nI suggest to create esbuild plugin, that:\n\n1. Parse test file AST\n2. Detect:\n - `vi.mock()`\n - `vi.unmock()`\n - `vi.doMock()`\n - `vi.doUnmock()`\n - `vi.importMock()`\n - `vi.importActual()`\n - `vi.hoisted()`\n3. Hoist mock calls\n4. Rewrite imports\n\n#### Example Transform Injectable\n\n**Before**:\n```typescript\nimport { MyService } from './my-service';\nimport { MyOtherService } from './my-other-service';\n\nvi.mock('./my-service', () => ({\n MyService:\n @Injectable({providedIn: 'root'})\n class {\n get() { return 'mock'; }\n }\n}));\n\nvi.mock('./my-other-service');\n\ntest(() => {\n const myService = new MyService();\n const myOtherService = new MyOtherService();\n});\n```\n\n**After**:\n```typescript\n// should initialize once in one environment\nconst __angularViMocks = (globalThis.__angular_vi_mocks ??= new Map\u003cstring, any>());\n\n__angularViMocks.set(\n './my-service',\n (() => ({\n MyService: class {\n get() { return 'mock'; }\n static ɵprov = {\n providedIn: 'root',\n factory: () => new this();\n };\n static ɵfac = () => new this(); \n }\n }))()\n);\n\n__angularViMocks.set(\n './my-other-service',\n (() => ({\n MyOtherService: class {\n get = vi.fn(); // maybe better to put `vi.fn()` to MyOtherService.prototype.get = vi.fn();\n static ɵprov = {\n providedIn: 'root',\n factory: () => new this();\n };\n static ɵfac = () => new this(); \n }\n }))()\n);\n\nimport * as __angularViActualMod1 from './my-service';\nimport * as __angularViActualMod2 from './my-other-service';\n\n// We should replace that import in every dependent chunk.\nconst { MyService } = __angularViMocks.get('./my-service') ?? __angularViActualMod1;\nconst { MyOtherService } = __angularViMocks.get('./my-other-service') ?? __angularViActualMod2;\n```\n\n### Requirements:\n\n- Must mock full implementation (not only metadata) for any modules, as `vi.mock()` does\n- Must stub components, services, modules, pipes, directives both metadata and implementation\n- Must preserve ESM live bindings semantics\n- Must preserve sourcemaps and coverage\n- Must not require runtime module loader\n\n### Alternatives considered\n\nCurrently we have no choice, but stay with slow and inefficient `jest` + `jest-preset-angular`. \n\nI've implemented deep-automocking infrastructure for `jest.mock()` [ng-automocks-jest](https://www.npmjs.com/package/ng-automocks-jest), it can stub any angular entities, both metadata and implementation.","author":{"url":"https://github.com/MillerSvt","@type":"Person","name":"MillerSvt"},"datePublished":"2026-03-02T06:54:33Z","url":"https://github.com/angular/angular-cli/issues/32641"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:4dad6fe5-f22d-8fcf-4ff6-1722b809bda4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D314:2E7EBB:37687BA:462C1A1:6B01E2B8 |
| html-safe-nonce | 283c62359dcc118521f6a2299359030c70c7d902249ef00712927068995616e1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzE0OjJFN0VCQjozNzY4N0JBOjQ2MkMxQTE6NkIwMUUyQjgiLCJ2aXNpdG9yX2lkIjoiNDc4NTg3ODA2MTExMzUzMzExMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 63cd875e46f3ea731c8120b534680ff9bbbef28a15d3f4a0fcf3f27add5209b6 |
| hovercard-subject-tag | issue:4010447619 |
| 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/32641/issue_layout |
| twitter:image | https://opengraph.githubassets.com/565620c0ca8b9b1cfc09b123fdb83e49d929d244bd449680ba86ef4d4619d719/angular/angular-cli/issues/32641 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/565620c0ca8b9b1cfc09b123fdb83e49d929d244bd449680ba86ef4d4619d719/angular/angular-cli/issues/32641 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? core Description With the new Angular unit-test builder (Vite), test files are bundled into unified chunks. Because of this ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | MillerSvt |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width