diff --git a/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts b/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts index 3f75d2038453..4207e9b6225f 100644 --- a/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts +++ b/packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts @@ -187,31 +187,36 @@ export async function getVitestBuildOptions( const mockPatchContents = ` import { vi } from 'vitest'; - const error = new Error( - 'The "vi.mock" and related methods are not supported for relative imports with the Angular unit-test system. ' + - 'Please use Angular TestBed for mocking dependencies.' - ); - - // Store original implementations - const { mock, doMock, importMock, unmock, doUnmock } = vi; - - function patch(original) { - return (path, ...args) => { - // Check if the path is a string and starts with a character that indicates a relative path. - if (typeof path === 'string' && /^[./]/.test(path)) { - throw error; - } - - // Call the original function for non-relative paths. - return original(path, ...args); - }; + const ANGULAR_VITEST_MOCK_PATCH = Symbol.for('@angular/cli/vitest-mock-patch'); + if (!globalThis[ANGULAR_VITEST_MOCK_PATCH]) { + globalThis[ANGULAR_VITEST_MOCK_PATCH] = true; + + const error = new Error( + 'The "vi.mock" and related methods are not supported for relative imports with the Angular unit-test system. ' + + 'Please use Angular TestBed for mocking dependencies.' + ); + + // Store original implementations + const { mock, doMock, importMock, unmock, doUnmock } = vi; + + function patch(original) { + return (path, ...args) => { + // Check if the path is a string and starts with a character that indicates a relative path. + if (typeof path === 'string' && /^[./]/.test(path)) { + throw error; + } + + // Call the original function for non-relative paths. + return original(path, ...args); + }; + } + + vi.mock = patch(mock); + vi.doMock = patch(doMock); + vi.importMock = patch(importMock); + vi.unmock = patch(unmock); + vi.doUnmock = patch(doUnmock); } - - vi.mock = patch(mock); - vi.doMock = patch(doMock); - vi.importMock = patch(importMock); - vi.unmock = patch(unmock); - vi.doUnmock = patch(doUnmock); `; return {