Skip to content

Commit be51753

Browse files
authored
fix(@angular/build): resolve Vitest artifacts when workspace root is aliased
In Vitest 5.1.0, `vitestConfig.root` inherits `viteConfig.root`, which resolves symlinks and Windows `subst` drives via `realpathSync`. Normalize paths under `vitestConfig.root` back to `workspaceRoot` in the in-memory provider plugin so internal setup files and build artifacts resolve consistently.
1 parent ab12e4f commit be51753

3 files changed

Lines changed: 126 additions & 7 deletions

File tree

‎packages/angular/build/src/builders/unit-test/runners/vitest/executor.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
updateExternalMetadata,
2020
} from '../../../../tools/vite/utils';
2121
import { assertIsError } from '../../../../utils/error';
22+
import { toPosixPath } from '../../../../utils/path';
2223
import {
2324
type FullResult,
2425
type IncrementalResult,
@@ -377,11 +378,15 @@ export class VitestExecutor implements TestExecutor {
377378
// Filter internal entries and setup files from the include list
378379
const internalEntries = ['angular:'];
379380
const setupFileSet = new Set(testSetupFiles);
380-
const include = [...this.testFileToEntryPoint.keys()].filter((entry) => {
381-
return (
382-
!internalEntries.some((internal) => entry.startsWith(internal)) && !setupFileSet.has(entry)
383-
);
384-
});
381+
const include: string[] = [];
382+
for (const entry of this.testFileToEntryPoint.keys()) {
383+
if (
384+
!internalEntries.some((internal) => entry.startsWith(internal)) &&
385+
!setupFileSet.has(entry)
386+
) {
387+
include.push(toPosixPath(path.relative(workspaceRoot, entry)));
388+
}
389+
}
385390
this.debugLog(DebugLogLevel.Verbose, 'Included test files (after filtering):', include);
386391

387392
const vitestConfig = {

‎packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,10 @@ async function loadResultFile(file: ResultFile): Promise<string> {
339339
}
340340

341341
export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[] {
342-
const { workspaceRoot, buildResultFiles, testFileToEntryPoint, setupFiles } = pluginOptions;
342+
const { buildResultFiles, setupFiles } = pluginOptions;
343+
let { workspaceRoot, testFileToEntryPoint } = pluginOptions;
343344
const isWindows = platform() === 'win32';
344-
const setupFileSet = new Set(
345+
let setupFileSet = new Set(
345346
setupFiles.map((file) =>
346347
toPosixPath(path.isAbsolute(file) ? file : path.join(workspaceRoot, file)),
347348
),
@@ -368,6 +369,29 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
368369
configureVitest(context: VitestPluginContext) {
369370
vitestConfig = context.vitest.config;
370371

372+
if (vitestConfig.root && vitestConfig.root !== toPosixPath(workspaceRoot)) {
373+
const previousRoot = workspaceRoot;
374+
workspaceRoot = vitestConfig.root;
375+
testFileToEntryPoint = new Map(
376+
Array.from(testFileToEntryPoint, ([file, entryPoint]) => [
377+
file.startsWith('angular:')
378+
? file
379+
: toPosixPath(path.join(workspaceRoot, path.relative(previousRoot, file))),
380+
entryPoint,
381+
]),
382+
);
383+
setupFileSet = new Set(
384+
setupFiles.map((file) =>
385+
toPosixPath(
386+
path.join(
387+
workspaceRoot,
388+
path.isAbsolute(file) ? path.relative(previousRoot, file) : file,
389+
),
390+
),
391+
),
392+
);
393+
}
394+
371395
// Vitest keys its file system module cache on the file found on disk. A test entry point is
372396
// served from its bundle instead, which also holds the application code it imports, so the
373397
// key must follow the served build output, or the cache would return a stale bundle.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import path from 'node:path';
10+
import { BuildOutputFileType } from '../../../../tools/esbuild/bundler-files';
11+
import { toPosixPath } from '../../../../utils/path';
12+
import type { ResultFile } from '../../../application/results';
13+
import { createVitestPlugins } from './plugins';
14+
15+
describe('createVitestPlugins', () => {
16+
it('resolves and loads setup and spec files when vitestConfig.root differs from workspaceRoot', async () => {
17+
const workspaceRoot = path.resolve('/logical-workspace/test-project');
18+
const physicalRoot = toPosixPath(path.resolve('/physical-disk/tmp/test-project'));
19+
const specFile = toPosixPath(path.join(workspaceRoot, 'src/app/app.spec.ts'));
20+
21+
const buildResultFiles = new Map<string, ResultFile>([
22+
[
23+
'init-compiler.js',
24+
{
25+
origin: 'memory',
26+
path: 'init-compiler.js',
27+
type: BuildOutputFileType.Root,
28+
contents: new TextEncoder().encode('console.log("init-compiler");'),
29+
hash: 'hash-init-compiler',
30+
},
31+
],
32+
[
33+
'spec-src-app-app-spec.js',
34+
{
35+
origin: 'memory',
36+
path: 'spec-src-app-app-spec.js',
37+
type: BuildOutputFileType.Root,
38+
contents: new TextEncoder().encode('console.log("spec");'),
39+
hash: 'hash-spec',
40+
},
41+
],
42+
]);
43+
44+
const testFileToEntryPoint = new Map<string, string>([[specFile, 'spec-src-app-app-spec']]);
45+
46+
const plugins = createVitestPlugins({
47+
workspaceRoot,
48+
projectSourceRoot: path.join(workspaceRoot, 'src'),
49+
projectName: 'test-project',
50+
buildResultFiles,
51+
testFileToEntryPoint,
52+
setupFiles: ['init-compiler.js'],
53+
});
54+
55+
const inMemoryPlugin = plugins.find((p) => p.name === 'angular:test-in-memory-provider');
56+
expect(inMemoryPlugin).toBeDefined();
57+
58+
const configureVitest = inMemoryPlugin?.configureVitest as
59+
((context: { vitest: { config: { root: string } } }) => void) | undefined;
60+
configureVitest?.({
61+
vitest: {
62+
config: {
63+
root: physicalRoot,
64+
},
65+
},
66+
});
67+
68+
const resolveId = inMemoryPlugin?.resolveId as (
69+
id: string,
70+
importer: string | undefined,
71+
) => string | undefined;
72+
const load = inMemoryPlugin?.load as (
73+
id: string,
74+
) => Promise<{ code: string; map?: unknown } | undefined>;
75+
76+
const expectedSetupId = `${physicalRoot}/init-compiler.js`;
77+
const resolvedSetupId = resolveId(expectedSetupId, undefined);
78+
expect(resolvedSetupId).toBe(expectedSetupId);
79+
80+
const loadedSetup = await load(expectedSetupId);
81+
expect(loadedSetup?.code).toBe('console.log("init-compiler");');
82+
83+
const expectedSpecId = `${physicalRoot}/src/app/app.spec.ts`;
84+
const resolvedSpecId = resolveId(expectedSpecId, undefined);
85+
expect(resolvedSpecId).toBe(expectedSpecId);
86+
87+
const loadedSpec = await load(expectedSpecId);
88+
expect(loadedSpec?.code).toBe('console.log("spec");');
89+
});
90+
});

0 commit comments

Comments
 (0)