fix(@angular-devkit/build-angular): do not fail compilation when spec pattern does not match

Previously, we failed the compilation when the specified patterns did not match any spec file. This breaks the case were users configure Karma to not fail on empty test suit.

Closes #24644
This commit is contained in:
Alan Agius 2023-02-06 13:37:27 +00:00 committed by angular-robot[bot]
parent 88fddc0503
commit d9c697b2bc
2 changed files with 11 additions and 21 deletions

View File

@ -9,10 +9,10 @@
import assert from 'assert';
import { PathLike, constants, promises as fs } from 'fs';
import glob, { hasMagic } from 'glob';
import { pluginName } from 'mini-css-extract-plugin';
import { basename, dirname, extname, join, relative } from 'path';
import { promisify } from 'util';
import type { Compilation, Compiler } from 'webpack';
import { addError } from '../../utils/webpack-diagnostics';
const globPromise = promisify(glob);
@ -49,23 +49,21 @@ export class FindTestsPlugin {
// Add tests files are part of the entry-point.
webpackOptions.entry = async () => {
const specFiles = await findTests(include, exclude, workspaceRoot, projectSourceRoot);
if (!specFiles.length) {
assert(this.compilation, 'Compilation cannot be undefined.');
addError(
this.compilation,
`Specified patterns: "${include.join(', ')}" did not match any spec files.`,
);
}
const entrypoints = await entry;
const entrypoint = entrypoints['main'];
if (!entrypoint.import) {
throw new Error(`Cannot find 'main' entrypoint.`);
}
originalImport ??= entrypoint.import;
entrypoint.import = [...originalImport, ...specFiles];
if (specFiles.length) {
originalImport ??= entrypoint.import;
entrypoint.import = [...originalImport, ...specFiles];
} else {
assert(this.compilation, 'Compilation cannot be undefined.');
this.compilation
.getLogger(pluginName)
.error(`Specified patterns: "${include.join(', ')}" did not match any spec files.`);
}
return entrypoints;
};

View File

@ -17,16 +17,8 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
include: ['abc.spec.ts', 'def.spec.ts'],
});
const { result, logs } = await harness.executeOnce();
const { result } = await harness.executeOnce();
expect(result?.success).toBeFalse();
expect(logs).toContain(
jasmine.objectContaining({
level: 'error',
message: jasmine.stringContaining(
'Specified patterns: "abc.spec.ts, def.spec.ts" did not match any spec files.',
),
}),
);
});
[