Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,10 @@ async function loadResultFile(file: ResultFile): Promise<string> {
}

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)),
),
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, ResultFile>([
[
'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<string, string>([[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");');
});
});
Loading