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 471f5dea467b..6af8b0a13111 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 @@ -19,6 +19,7 @@ import { updateExternalMetadata, } from '../../../../tools/vite/utils'; import { assertIsError } from '../../../../utils/error'; +import { toPosixPath } from '../../../../utils/path'; import { type FullResult, type IncrementalResult, @@ -377,11 +378,15 @@ export class VitestExecutor implements TestExecutor { // Filter internal entries and setup files from the include list const internalEntries = ['angular:']; const setupFileSet = new Set(testSetupFiles); - const include = [...this.testFileToEntryPoint.keys()].filter((entry) => { - return ( - !internalEntries.some((internal) => entry.startsWith(internal)) && !setupFileSet.has(entry) - ); - }); + const include: string[] = []; + for (const entry of this.testFileToEntryPoint.keys()) { + if ( + !internalEntries.some((internal) => entry.startsWith(internal)) && + !setupFileSet.has(entry) + ) { + include.push(toPosixPath(path.relative(workspaceRoot, entry))); + } + } this.debugLog(DebugLogLevel.Verbose, 'Included test files (after filtering):', include); const vitestConfig = { diff --git a/packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts b/packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts index 37aa4a9828a6..f430c3094b10 100644 --- a/packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts +++ b/packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts @@ -339,9 +339,10 @@ async function loadResultFile(file: ResultFile): Promise { } export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[] { - const { workspaceRoot, buildResultFiles, testFileToEntryPoint, setupFiles } = pluginOptions; + const { buildResultFiles, setupFiles } = pluginOptions; + let { workspaceRoot, testFileToEntryPoint } = pluginOptions; const isWindows = platform() === 'win32'; - const setupFileSet = new Set( + let setupFileSet = new Set( setupFiles.map((file) => toPosixPath(path.isAbsolute(file) ? file : path.join(workspaceRoot, file)), ), @@ -368,6 +369,29 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[] configureVitest(context: VitestPluginContext) { vitestConfig = context.vitest.config; + if (vitestConfig.root && vitestConfig.root !== toPosixPath(workspaceRoot)) { + const previousRoot = workspaceRoot; + workspaceRoot = vitestConfig.root; + testFileToEntryPoint = new Map( + Array.from(testFileToEntryPoint, ([file, entryPoint]) => [ + file.startsWith('angular:') + ? file + : toPosixPath(path.join(workspaceRoot, path.relative(previousRoot, file))), + entryPoint, + ]), + ); + setupFileSet = new Set( + setupFiles.map((file) => + toPosixPath( + path.join( + workspaceRoot, + path.isAbsolute(file) ? path.relative(previousRoot, file) : file, + ), + ), + ), + ); + } + // Vitest keys its file system module cache on the file found on disk. A test entry point is // served from its bundle instead, which also holds the application code it imports, so the // key must follow the served build output, or the cache would return a stale bundle. diff --git a/packages/angular/build/src/builders/unit-test/runners/vitest/plugins_spec.ts b/packages/angular/build/src/builders/unit-test/runners/vitest/plugins_spec.ts new file mode 100644 index 000000000000..b097dfb48481 --- /dev/null +++ b/packages/angular/build/src/builders/unit-test/runners/vitest/plugins_spec.ts @@ -0,0 +1,90 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.dev/license + */ + +import path from 'node:path'; +import { BuildOutputFileType } from '../../../../tools/esbuild/bundler-files'; +import { toPosixPath } from '../../../../utils/path'; +import type { ResultFile } from '../../../application/results'; +import { createVitestPlugins } from './plugins'; + +describe('createVitestPlugins', () => { + it('resolves and loads setup and spec files when vitestConfig.root differs from workspaceRoot', async () => { + const workspaceRoot = path.resolve('/logical-workspace/test-project'); + const physicalRoot = toPosixPath(path.resolve('/physical-disk/tmp/test-project')); + const specFile = toPosixPath(path.join(workspaceRoot, 'src/app/app.spec.ts')); + + const buildResultFiles = new Map([ + [ + 'init-compiler.js', + { + origin: 'memory', + path: 'init-compiler.js', + type: BuildOutputFileType.Root, + contents: new TextEncoder().encode('console.log("init-compiler");'), + hash: 'hash-init-compiler', + }, + ], + [ + 'spec-src-app-app-spec.js', + { + origin: 'memory', + path: 'spec-src-app-app-spec.js', + type: BuildOutputFileType.Root, + contents: new TextEncoder().encode('console.log("spec");'), + hash: 'hash-spec', + }, + ], + ]); + + const testFileToEntryPoint = new Map([[specFile, 'spec-src-app-app-spec']]); + + const plugins = createVitestPlugins({ + workspaceRoot, + projectSourceRoot: path.join(workspaceRoot, 'src'), + projectName: 'test-project', + buildResultFiles, + testFileToEntryPoint, + setupFiles: ['init-compiler.js'], + }); + + const inMemoryPlugin = plugins.find((p) => p.name === 'angular:test-in-memory-provider'); + expect(inMemoryPlugin).toBeDefined(); + + const configureVitest = inMemoryPlugin?.configureVitest as + ((context: { vitest: { config: { root: string } } }) => void) | undefined; + configureVitest?.({ + vitest: { + config: { + root: physicalRoot, + }, + }, + }); + + const resolveId = inMemoryPlugin?.resolveId as ( + id: string, + importer: string | undefined, + ) => string | undefined; + const load = inMemoryPlugin?.load as ( + id: string, + ) => Promise<{ code: string; map?: unknown } | undefined>; + + const expectedSetupId = `${physicalRoot}/init-compiler.js`; + const resolvedSetupId = resolveId(expectedSetupId, undefined); + expect(resolvedSetupId).toBe(expectedSetupId); + + const loadedSetup = await load(expectedSetupId); + expect(loadedSetup?.code).toBe('console.log("init-compiler");'); + + const expectedSpecId = `${physicalRoot}/src/app/app.spec.ts`; + const resolvedSpecId = resolveId(expectedSpecId, undefined); + expect(resolvedSpecId).toBe(expectedSpecId); + + const loadedSpec = await load(expectedSpecId); + expect(loadedSpec?.code).toBe('console.log("spec");'); + }); +});