From 1cb3324bfb072e9c3300dd9753746cad3b36deb0 Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Mon, 17 Nov 2025 09:22:04 +0000 Subject: [PATCH] fix(@angular/build): configure Vitest cache to use Angular cache Configures Vitest to use the Angular cache directory instead of its default location within `node_modules/.vite`. This change ensures that Vitest's cache is managed consistently with other Angular build artifacts, avoiding unnecessary writes to `node_modules`. --- .../unit-test/runners/vitest/executor.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/angular/build/src/builders/unit-test/runners/vitest/executor.ts b/packages/angular/build/src/builders/unit-test/runners/vitest/executor.ts index 8d09550b671a..bac8e389c4c5 100644 --- a/packages/angular/build/src/builders/unit-test/runners/vitest/executor.ts +++ b/packages/angular/build/src/builders/unit-test/runners/vitest/executor.ts @@ -8,7 +8,7 @@ import type { BuilderOutput } from '@angular-devkit/architect'; import assert from 'node:assert'; -import path from 'node:path'; +import path, { join } from 'node:path'; import type { Vitest } from 'vitest/node'; import { DevServerExternalResultMetadata, @@ -150,6 +150,10 @@ export class VitestExecutor implements TestExecutor { watch, browserViewport, ui, + projectRoot, + runnerConfig, + projectSourceRoot, + cacheOptions, } = this.options; const projectName = this.projectName; @@ -171,7 +175,7 @@ export class VitestExecutor implements TestExecutor { const browserOptions = await setupBrowserConfiguration( browsers, debug, - this.options.projectSourceRoot, + projectSourceRoot, browserViewport, ); if (browserOptions.errors?.length) { @@ -186,7 +190,7 @@ export class VitestExecutor implements TestExecutor { const testSetupFiles = this.prepareSetupFiles(); const projectPlugins = createVitestPlugins({ workspaceRoot, - projectSourceRoot: this.options.projectSourceRoot, + projectSourceRoot, projectName, buildResultFiles: this.buildResultFiles, testFileToEntryPoint: this.testFileToEntryPoint, @@ -200,10 +204,9 @@ export class VitestExecutor implements TestExecutor { } : {}; - const runnerConfig = this.options.runnerConfig; const externalConfigPath = runnerConfig === true - ? await findVitestBaseConfig([this.options.projectRoot, this.options.workspaceRoot]) + ? await findVitestBaseConfig([projectRoot, workspaceRoot]) : runnerConfig; return startVitest( @@ -214,12 +217,15 @@ export class VitestExecutor implements TestExecutor { root: workspaceRoot, project: projectName, outputFile, + cache: cacheOptions.enabled ? undefined : false, testNamePattern: this.options.filter, watch, ui, ...debugOptions, }, { + // Note `.vitest` is auto appended to the path. + cacheDir: cacheOptions.path, server: { // Disable the actual file watcher. The boolean watch option above should still // be enabled as it controls other internal behavior related to rerunning tests. @@ -230,7 +236,7 @@ export class VitestExecutor implements TestExecutor { browser: browserOptions.browser, coverage, projectName, - projectSourceRoot: this.options.projectSourceRoot, + projectSourceRoot, optimizeDepsInclude: this.externalMetadata.implicitBrowser, reporters, setupFiles: testSetupFiles,