Skip to content

Commit 22cba79

Browse files
committedJan 9, 2023
fix(@angular-devkit/build-angular): provide an option to exclude specs in Karma builder
With this change we add an `exclude` option to the Karma builder to provide a way to exclude certain specs from the compilation. This is useful, when having integration, e2e and unit tests with the same suffix. Closes #24472 (cherry picked from commit e9987cf)
1 parent 79578fc commit 22cba79

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed
 

‎goldens/public-api/angular_devkit/build_angular/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export interface KarmaBuilderOptions {
176176
browsers?: string;
177177
codeCoverage?: boolean;
178178
codeCoverageExclude?: string[];
179+
exclude?: string[];
179180
fileReplacements?: FileReplacement_2[];
180181
include?: string[];
181182
inlineStyleLanguage?: InlineStyleLanguage_2;

‎packages/angular_devkit/build_angular/src/builders/karma/find-tests-plugin.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const PLUGIN_NAME = 'angular-find-tests-plugin';
2323

2424
export interface FindTestsPluginOptions {
2525
include?: string[];
26+
exclude?: string[];
2627
workspaceRoot: string;
2728
projectSourceRoot: string;
2829
}
@@ -33,7 +34,12 @@ export class FindTestsPlugin {
3334
constructor(private options: FindTestsPluginOptions) {}
3435

3536
apply(compiler: Compiler): void {
36-
const { include = ['**/*.spec.ts'], projectSourceRoot, workspaceRoot } = this.options;
37+
const {
38+
include = ['**/*.spec.ts'],
39+
exclude = [],
40+
projectSourceRoot,
41+
workspaceRoot,
42+
} = this.options;
3743
const webpackOptions = compiler.options;
3844
const entry =
3945
typeof webpackOptions.entry === 'function' ? webpackOptions.entry() : webpackOptions.entry;
@@ -42,7 +48,7 @@ export class FindTestsPlugin {
4248

4349
// Add tests files are part of the entry-point.
4450
webpackOptions.entry = async () => {
45-
const specFiles = await findTests(include, workspaceRoot, projectSourceRoot);
51+
const specFiles = await findTests(include, exclude, workspaceRoot, projectSourceRoot);
4652

4753
if (!specFiles.length) {
4854
assert(this.compilation, 'Compilation cannot be undefined.');
@@ -73,12 +79,13 @@ export class FindTestsPlugin {
7379

7480
// go through all patterns and find unique list of files
7581
async function findTests(
76-
patterns: string[],
82+
include: string[],
83+
exclude: string[],
7784
workspaceRoot: string,
7885
projectSourceRoot: string,
7986
): Promise<string[]> {
80-
const matchingTestsPromises = patterns.map((pattern) =>
81-
findMatchingTests(pattern, workspaceRoot, projectSourceRoot),
87+
const matchingTestsPromises = include.map((pattern) =>
88+
findMatchingTests(pattern, exclude, workspaceRoot, projectSourceRoot),
8289
);
8390
const files = await Promise.all(matchingTestsPromises);
8491

@@ -90,6 +97,7 @@ const normalizePath = (path: string): string => path.replace(/\\/g, '/');
9097

9198
async function findMatchingTests(
9299
pattern: string,
100+
ignore: string[],
93101
workspaceRoot: string,
94102
projectSourceRoot: string,
95103
): Promise<string[]> {
@@ -132,7 +140,7 @@ async function findMatchingTests(
132140
root: projectSourceRoot,
133141
nomount: true,
134142
absolute: true,
135-
ignore: ['**/node_modules/**'],
143+
ignore: ['**/node_modules/**', ...ignore],
136144
});
137145
}
138146

‎packages/angular_devkit/build_angular/src/builders/karma/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export function execute(
134134
webpackConfig.plugins.push(
135135
new FindTestsPlugin({
136136
include: options.include,
137+
exclude: options.exclude,
137138
workspaceRoot: context.workspaceRoot,
138139
projectSourceRoot: path.join(context.workspaceRoot, sourceRoot),
139140
}),

‎packages/angular_devkit/build_angular/src/builders/karma/schema.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,15 @@
141141
"type": "string"
142142
},
143143
"default": ["**/*.spec.ts"],
144-
"description": "Globs of files to include, relative to workspace or project root. \nThere are 2 special cases:\n - when a path to directory is provided, all spec files ending \".spec.@(ts|tsx)\" will be included\n - when a path to a file is provided, and a matching spec file exists it will be included instead."
144+
"description": "Globs of files to include, relative to project root. \nThere are 2 special cases:\n - when a path to directory is provided, all spec files ending \".spec.@(ts|tsx)\" will be included\n - when a path to a file is provided, and a matching spec file exists it will be included instead."
145+
},
146+
"exclude": {
147+
"type": "array",
148+
"items": {
149+
"type": "string"
150+
},
151+
"default": [],
152+
"description": "Globs of files to exclude, relative to the project root."
145153
},
146154
"sourceMap": {
147155
"description": "Output source maps for scripts and styles. For more information, see https://angular.io/guide/workspace-config#source-map-configuration.",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.io/license
7+
*/
8+
9+
import { execute } from '../../index';
10+
import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
13+
describe('Option: "exclude"', () => {
14+
beforeEach(async () => {
15+
await harness.writeFiles({
16+
'src/app/error.spec.ts': `
17+
describe('Error spec', () => {
18+
it('should error', () => {
19+
expect(false).toBe(true);
20+
});
21+
});`,
22+
});
23+
});
24+
25+
it(`should not exclude any spec when exclude is not supplied`, async () => {
26+
harness.useTarget('test', {
27+
...BASE_OPTIONS,
28+
});
29+
30+
const { result } = await harness.executeOnce();
31+
expect(result?.success).toBeFalse();
32+
});
33+
34+
it(`should exclude spec that matches the 'exclude' pattern`, async () => {
35+
harness.useTarget('test', {
36+
...BASE_OPTIONS,
37+
exclude: ['**/error.spec.ts'],
38+
});
39+
40+
const { result } = await harness.executeOnce();
41+
expect(result?.success).toBeTrue();
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)
Please sign in to comment.