Repository navigation
Allow using vi.mock (and related) functions in tests #32641
Description
Activity
- addedgemini-triagedLabel noting that an issue has been triaged by geminiLabel noting that an issue has been triaged by gemini
on Mar 2, 2026 I did run into the same issue. I did mock an import (
import { jwtDecode } from 'jwt-decode';) for test which now does not work anymore.vi.mock('jwt-decode', () => ({ jwtDecode: vi.fn(), })); it(...){ //... (jwtDecode as ReturnType<typeof vi.fn>).mockReturnValue({ realm_access: { roles: ['admin', 'user'], }, }); //... }- addedgemini-triagedLabel noting that an issue has been triaged by geminiLabel noting that an issue has been triaged by gemini
on Mar 2, 2026 - addedaction: cleanupThe PR is in need of cleanup, either due to needing a rebase or in response to comments from reviewsThe PR is in need of cleanup, either due to needing a rebase or in response to comments from reviewsand removedgemini-triagedLabel noting that an issue has been triaged by geminiLabel noting that an issue has been triaged by geminiaction: cleanupThe PR is in need of cleanup, either due to needing a rebase or in response to comments from reviewsThe PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews
on Mar 2, 2026 Related to #31609
- linked a pull request that will close this issuefix(@angular/build): conditionally allow `vi.mock` for non-relative imports #32655
on Mar 3, 2026 @alan-agius4 let's continue here.
Instead TestBed.overrideProvider or TestBed.configureTestingModule should be used as is the supported way
TestBed is not enough to mocking. As described here, it cannot mock implementation. It can only override metadata. Also TestBed cannot mock platform providers, but only root providers.
If you want to stay framework agnostic, please allow us to add esbuild plugin to implement this thing by third-party plugin. We need to intercept
vi.mockand related calls at compile-time, and transform it for tests bundle. I can createng-automocks-vitestplugin and implement this mocks on my own.Reacted by Ramiro Jr. Franco, Jaime Burgos, Ilya Borisov, Maciej Niedzielski, Diandson C. Ramos and VeselyT- removed a link to a pull requestfix(@angular/build): conditionally allow `vi.mock` for non-relative imports #32655
on Mar 4, 2026 Can I try to make pull request for this to push it forward?
Reacted by HerrDerb
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:
Currently if we use
vi.mockfunction for automocking component/directives/services/pipes/etc..., we got an error:TestBed overrides are insufficient because:
Proposed solution
Instead of runtime interception (like Vitest normally does), Angular test builder should:
vi.mock()calls at compile time (in spec files, and in setupFiles)I suggest to create esbuild plugin, that:
vi.mock()vi.unmock()vi.doMock()vi.doUnmock()vi.importMock()vi.importActual()vi.hoisted()Example Transform Injectable
Before:
After:
Requirements:
vi.mock()doesAlternatives considered
Currently we have no choice, but stay with slow and inefficient
jest+jest-preset-angular.I've implemented deep-automocking infrastructure for
jest.mock()ng-automocks-jest, it can stub any angular entities, both metadata and implementation.