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
1 change: 1 addition & 0 deletions packages/angular/build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ jasmine_test(
name = "unit-test_integration_tests",
size = "medium",
data = [":unit-test_integration_test_lib"],
flaky = True,
shard_count = 5,
)

Expand Down
11 changes: 9 additions & 2 deletions packages/angular/build/src/builders/karma/find-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ export async function findTests(
interface TestEntrypointsOptions {
projectSourceRoot: string;
workspaceRoot: string;
removeTestExtension?: boolean;
}

/** Generate unique bundle names for a set of test files. */
export function getTestEntrypoints(
testFiles: string[],
{ projectSourceRoot, workspaceRoot }: TestEntrypointsOptions,
{ projectSourceRoot, workspaceRoot, removeTestExtension }: TestEntrypointsOptions,
): Map<string, string> {
const seen = new Set<string>();

Expand All @@ -46,7 +47,13 @@ export function getTestEntrypoints(
.replace(/^[./\\]+/, '')
// Replace any path separators with dashes.
.replace(/[/\\]/g, '-');
const baseName = `spec-${basename(relativePath, extname(relativePath))}`;

let fileName = basename(relativePath, extname(relativePath));
if (removeTestExtension) {
fileName = fileName.replace(/\.(spec|test)$/, '');
}

const baseName = `spec-${fileName}`;
let uniqueName = baseName;
let suffix = 2;
while (seen.has(uniqueName)) {
Expand Down
30 changes: 30 additions & 0 deletions packages/angular/build/src/builders/karma/find-tests_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ describe('getTestEntrypoints', () => {
]),
);
});

describe('with removeTestExtension enabled', () => {
function getEntrypoints(workspaceRelative: string[], sourceRootRelative: string[] = []) {
return getTestEntrypoints(
[
...workspaceRelative.map((p) => joinWithSeparator(options.workspaceRoot, p)),
...sourceRootRelative.map((p) => joinWithSeparator(options.projectSourceRoot, p)),
],
{ ...options, removeTestExtension: true },
);
}

it('removes .spec extension', () => {
expect(getEntrypoints(['a/b.spec.js'], ['c/d.spec.js'])).toEqual(
new Map<string, string>([
['spec-a-b', joinWithSeparator(options.workspaceRoot, 'a/b.spec.js')],
['spec-c-d', joinWithSeparator(options.projectSourceRoot, 'c/d.spec.js')],
]),
);
});

it('removes .test extension', () => {
expect(getEntrypoints(['a/b.test.js'], ['c/d.test.js'])).toEqual(
new Map<string, string>([
['spec-a-b', joinWithSeparator(options.workspaceRoot, 'a/b.test.js')],
['spec-c-d', joinWithSeparator(options.projectSourceRoot, 'c/d.test.js')],
]),
);
});
});
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ export async function getVitestBuildOptions(
);
}

const entryPoints = getTestEntrypoints(testFiles, { projectSourceRoot, workspaceRoot });
const entryPoints = getTestEntrypoints(testFiles, {
projectSourceRoot,
workspaceRoot,
removeTestExtension: true,
});
entryPoints.set('init-testbed', 'angular:test-bed-init');

const buildOptions: Partial<ApplicationBuilderInternalOptions> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ export class VitestExecutor implements TestExecutor {
if (this.testFileToEntryPoint.size === 0) {
const { include, exclude = [], workspaceRoot, projectSourceRoot } = this.options;
const testFiles = await findTests(include, exclude, workspaceRoot, projectSourceRoot);
const entryPoints = getTestEntrypoints(testFiles, { projectSourceRoot, workspaceRoot });
const entryPoints = getTestEntrypoints(testFiles, {
projectSourceRoot,
workspaceRoot,
removeTestExtension: true,
});
for (const [entryPoint, testFile] of entryPoints) {
this.testFileToEntryPoint.set(testFile, entryPoint);
this.entryPointToTestFile.set(entryPoint + '.js', testFile);
Expand All @@ -90,6 +94,7 @@ export class VitestExecutor implements TestExecutor {
if (source) {
modifiedSourceFiles.add(source);
}
vitest.invalidateFile(toPosixPath(path.join(this.options.workspaceRoot, modifiedFile)));
}

const specsToRerun = [];
Expand Down Expand Up @@ -162,16 +167,15 @@ export class VitestExecutor implements TestExecutor {
name: 'angular:test-in-memory-provider',
enforce: 'pre',
resolveId: (id, importer) => {
if (importer && id.startsWith('.')) {
if (importer && (id[0] === '.' || id[0] === '/')) {
let fullPath;
let relativePath;
if (this.testFileToEntryPoint.has(importer)) {
fullPath = toPosixPath(path.join(this.options.workspaceRoot, id));
relativePath = path.normalize(id);
} else {
fullPath = toPosixPath(path.join(path.dirname(importer), id));
relativePath = path.relative(this.options.workspaceRoot, fullPath);
}

const relativePath = path.relative(this.options.workspaceRoot, fullPath);
if (this.buildResultFiles.has(toPosixPath(relativePath))) {
return fullPath;
}
Expand Down Expand Up @@ -201,6 +205,12 @@ export class VitestExecutor implements TestExecutor {
let outputPath;
if (entryPoint) {
outputPath = entryPoint + '.js';

// To support coverage exclusion of the actual test file, the virtual
// test entry point only references the built and bundled intermediate file.
return {
code: `import "./${outputPath}";`,
};
} else {
// Attempt to load as a built artifact.
const relativePath = path.relative(this.options.workspaceRoot, id);
Expand Down Expand Up @@ -247,15 +257,6 @@ export class VitestExecutor implements TestExecutor {
},
],
});

// Adjust coverage excludes to not include the otherwise automatically inserted included unit tests.
// Vite does this as a convenience but is problematic for the bundling strategy employed by the
// builder's test setup. To workaround this, the excludes are adjusted here to only automatically
// exclude the TypeScript source test files.
project.config.coverage.exclude = [
...(codeCoverage?.exclude ?? []),
'**/*.{test,spec}.?(c|m)ts',
];
},
},
];
Expand Down Expand Up @@ -343,7 +344,8 @@ function generateCoverageOption(
return {
enabled: true,
excludeAfterRemap: true,
// Special handling for `reporter` due to an undefined value causing upstream failures
// Special handling for `exclude`/`reporters` due to an undefined value causing upstream failures
...(codeCoverage.exclude ? { exclude: codeCoverage.exclude } : {}),
...(codeCoverage.reporters
? ({ reporter: codeCoverage.reporters } satisfies VitestCoverageOption)
: {}),
Expand Down
3 changes: 1 addition & 2 deletions packages/angular/build/src/builders/unit-test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
"description": "Globs to exclude from code coverage.",
"items": {
"type": "string"
},
"default": []
}
},
"codeCoverageReporters": {
"type": "array",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '../setup';

describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
xdescribe('Option: "watch"', () => {
describe('Option: "watch"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});
Expand Down