Skip to content

Commit 2229539

Browse files
committed
fixup! fix(@angular/build): resolve Vitest artifacts when workspace root is aliased
1 parent 8a9fa18 commit 2229539

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,21 @@ async function loadResultFile(file: ResultFile): Promise<string> {
338338
return readFile(file.source, 'utf-8');
339339
}
340340

341+
function rebasePath(filePath: string, fromRoot: string, toRoot: string): string {
342+
if (path.isAbsolute(filePath)) {
343+
const relativePath = path.relative(fromRoot, filePath);
344+
if (
345+
relativePath !== '..' &&
346+
!relativePath.startsWith('..' + path.sep) &&
347+
!path.isAbsolute(relativePath)
348+
) {
349+
return path.join(toRoot, relativePath);
350+
}
351+
}
352+
353+
return filePath;
354+
}
355+
341356
export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[] {
342357
const { workspaceRoot, buildResultFiles, testFileToEntryPoint, setupFiles } = pluginOptions;
343358
const isWindows = platform() === 'win32';
@@ -348,20 +363,13 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
348363
);
349364
let vitestConfig: ResolvedConfig | undefined;
350365

351-
const normalizeToWorkspace = (filePath: string): string => {
352-
if (vitestConfig?.root && path.isAbsolute(filePath)) {
353-
const relativeToVitestRoot = path.relative(vitestConfig.root, filePath);
354-
if (
355-
relativeToVitestRoot !== '..' &&
356-
!relativeToVitestRoot.startsWith('..' + path.sep) &&
357-
!path.isAbsolute(relativeToVitestRoot)
358-
) {
359-
return path.join(workspaceRoot, relativeToVitestRoot);
360-
}
361-
}
366+
const normalizeToWorkspace = (filePath: string): string =>
367+
vitestConfig?.root ? rebasePath(filePath, vitestConfig.root, workspaceRoot) : filePath;
362368

363-
return filePath;
364-
};
369+
const normalizeToVitestRoot = (filePath: string): string =>
370+
toPosixPath(
371+
vitestConfig?.root ? rebasePath(filePath, workspaceRoot, vitestConfig.root) : filePath,
372+
);
365373

366374
/** The build output path served for a module, whether it is a test entry point or a built artifact. */
367375
const getOutputPath = (id: string): string => {
@@ -424,7 +432,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
424432
resolveId: (id: string, importer: string | undefined) => {
425433
// Fast path for test entry points.
426434
if (testFileToEntryPoint.has(id)) {
427-
return id;
435+
return normalizeToVitestRoot(id);
428436
}
429437

430438
// Workaround for Vitest in Windows when a fully qualified absolute path is provided with
@@ -466,13 +474,13 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
466474
fullPath = toPosixPath(fullPath);
467475

468476
if (testFileToEntryPoint.has(fullPath)) {
469-
return fullPath;
477+
return normalizeToVitestRoot(fullPath);
470478
}
471479

472480
// Check if the resolved path corresponds to a known build artifact.
473481
const relativePath = path.relative(workspaceRoot, fullPath);
474482
if (buildResultFiles.has(toPosixPath(relativePath))) {
475-
return fullPath;
483+
return normalizeToVitestRoot(fullPath);
476484
}
477485

478486
// If the module cannot be resolved from the build artifacts, let other plugins handle it.

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,18 @@ describe('createVitestPlugins', () => {
7373
id: string,
7474
) => Promise<{ code: string; map?: unknown } | undefined>;
7575

76-
const expectedSetupId = toPosixPath(path.join(workspaceRoot, 'init-compiler.js'));
77-
const resolvedSetupId = resolveId(`${physicalRoot}/init-compiler.js`, undefined);
76+
const expectedSetupId = `${physicalRoot}/init-compiler.js`;
77+
const resolvedSetupId = resolveId(expectedSetupId, undefined);
7878
expect(resolvedSetupId).toBe(expectedSetupId);
7979

8080
const loadedSetup = await load(expectedSetupId);
8181
expect(loadedSetup?.code).toBe('console.log("init-compiler");');
8282

83-
const resolvedSpecId = resolveId(`${physicalRoot}/src/app/app.spec.ts`, undefined);
84-
expect(resolvedSpecId).toBe(specFile);
83+
const expectedSpecId = `${physicalRoot}/src/app/app.spec.ts`;
84+
const resolvedSpecId = resolveId(expectedSpecId, undefined);
85+
expect(resolvedSpecId).toBe(expectedSpecId);
8586

86-
const loadedSpec = await load(specFile);
87+
const loadedSpec = await load(expectedSpecId);
8788
expect(loadedSpec?.code).toBe('console.log("spec");');
8889
});
8990
});

0 commit comments

Comments
 (0)