René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:4dad6fe5-f22d-8fcf-4ff6-1722b809bda4
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD314:2E7EBB:37687BA:462C1A1:6B01E2B8
html-safe-nonce283c62359dcc118521f6a2299359030c70c7d902249ef00712927068995616e1
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzE0OjJFN0VCQjozNzY4N0JBOjQ2MkMxQTE6NkIwMUUyQjgiLCJ2aXNpdG9yX2lkIjoiNDc4NTg3ODA2MTExMzUzMzExMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac63cd875e46f3ea731c8120b534680ff9bbbef28a15d3f4a0fcf3f27add5209b6
hovercard-subject-tagissue:4010447619
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular-cli/32641/issue_layout
twitter:imagehttps://opengraph.githubassets.com/565620c0ca8b9b1cfc09b123fdb83e49d929d244bd449680ba86ef4d4619d719/angular/angular-cli/issues/32641
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/565620c0ca8b9b1cfc09b123fdb83e49d929d244bd449680ba86ef4d4619d719/angular/angular-cli/issues/32641
og:image:altWhich @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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameMillerSvt
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
go-importgithub.com/angular/angular-cli git https://github.com/angular/angular-cli.git
octolytics-dimension-user_id139426
octolytics-dimension-user_loginangular
octolytics-dimension-repository_id36891867
octolytics-dimension-repository_nwoangular/angular-cli
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id36891867
octolytics-dimension-repository_network_root_nwoangular/angular-cli
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/angular/angular-cli/issues/32641#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular-cli%2Fissues%2F32641
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular-cli%2Fissues%2F32641
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=angular%2Fangular-cli
Reloadhttps://github.com/angular/angular-cli/issues/32641
Reloadhttps://github.com/angular/angular-cli/issues/32641
Reloadhttps://github.com/angular/angular-cli/issues/32641
angularhttps://github.com/angular
angular-clihttps://github.com/angular/angular-cli
Codehttps://github.com/angular/angular-cli
Issues198 (198)https://github.com/angular/angular-cli/issues
Pull requests23 (23)https://github.com/angular/angular-cli/pulls
Actionshttps://github.com/angular/angular-cli/actions
Security and quality8 (8)https://github.com/angular/angular-cli/security
Insightshttps://github.com/angular/angular-cli/insights
https://github.com/angular/angular-cli
New issuehttps://github.com/login?return_to=https://github.com/angular/angular-cli/issues/32641
New issuehttps://github.com/login?return_to=https://github.com/angular/angular-cli/issues/32641
Allow using vi.mock (and related) functions in testshttps://github.com/angular/angular-cli/issues/32641#top
angular/build:unit-testhttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22angular%2Fbuild%3Aunit-test%22
area: @angular/buildhttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22area%3A%20%40angular%2Fbuild%22
needsTriagehttps://github.com/angular/angular-cli/milestone/11
https://github.com/MillerSvt
MillerSvthttps://github.com/MillerSvt
on Mar 2, 2026https://github.com/angular/angular-cli/issues/32641#issue-4010447619
ng-automocks-jesthttps://www.npmjs.com/package/ng-automocks-jest
angular-robothttps://github.com/apps/angular-robot
gemini-triagedLabel noting that an issue has been triaged by geminihttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22gemini-triaged%22
on Mar 2, 2026https://github.com/angular/angular-cli/issues/32641#event-23188007519
ngbothttps://github.com/apps/ngbot
needsTriagehttps://github.com/angular/angular-cli/milestone/11
on Mar 2, 2026https://github.com/angular/angular-cli/issues/32641#event-23188007544
https://github.com/HerrDerb
HerrDerbhttps://github.com/HerrDerb
on Mar 2, 2026https://github.com/angular/angular-cli/issues/32641#issuecomment-3983451210
JeanMechehttps://github.com/JeanMeche
gemini-triagedLabel noting that an issue has been triaged by geminihttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22gemini-triaged%22
on Mar 2, 2026https://github.com/angular/angular-cli/issues/32641#event-23188007341
JeanMechehttps://github.com/JeanMeche
angular/angularhttps://github.com/angular/angular
on Mar 2, 2026https://github.com/angular/angular-cli/issues/32641#event-23188007775
alan-agius4https://github.com/alan-agius4
action: cleanupThe PR is in need of cleanup, either due to needing a rebase or in response to comments from reviewshttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22action%3A%20cleanup%22
area: @angular/buildhttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22area%3A%20%40angular%2Fbuild%22
angular/build:unit-testhttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22angular%2Fbuild%3Aunit-test%22
gemini-triagedLabel noting that an issue has been triaged by geminihttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22gemini-triaged%22
action: cleanupThe PR is in need of cleanup, either due to needing a rebase or in response to comments from reviewshttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22action%3A%20cleanup%22
on Mar 2, 2026https://github.com/angular/angular-cli/issues/32641#event-23188632756
https://github.com/gcoadour
gcoadourhttps://github.com/gcoadour
on Mar 2, 2026https://github.com/angular/angular-cli/issues/32641#issuecomment-3984302137
#31609https://github.com/angular/angular-cli/issues/31609
clydinhttps://github.com/clydin
fix(@angular/build): conditionally allow `vi.mock` for non-relative imports #32655https://github.com/angular/angular-cli/pull/32655
on Mar 3, 2026https://github.com/angular/angular-cli/issues/32641#event-23233864104
https://github.com/MillerSvt
MillerSvthttps://github.com/MillerSvt
on Mar 4, 2026https://github.com/angular/angular-cli/issues/32641#issuecomment-3995987589
@alan-agius4https://github.com/alan-agius4
clydinhttps://github.com/clydin
fix(@angular/build): conditionally allow `vi.mock` for non-relative imports #32655https://github.com/angular/angular-cli/pull/32655
on Mar 4, 2026https://github.com/angular/angular-cli/issues/32641#event-23259367996
https://github.com/MillerSvt
MillerSvthttps://github.com/MillerSvt
on May 28, 2026https://github.com/angular/angular-cli/issues/32641#issuecomment-4562577397
Sign up for freehttps://github.com/signup?return_to=https://github.com/angular/angular-cli/issues/32641
Sign in to commenthttps://github.com/login?return_to=https://github.com/angular/angular-cli/issues/32641
angular/build:unit-testhttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22angular%2Fbuild%3Aunit-test%22
area: @angular/buildhttps://github.com/angular/angular-cli/issues?q=state%3Aopen%20label%3A%22area%3A%20%40angular%2Fbuild%22
needsTriageNo due datehttps://github.com/angular/angular-cli/milestone/11
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.