Skip to content

Commit bffaa77

Browse files
dgp1130alan-agius4
authored andcommitted
refactor: move findTestFiles to a common directory where it can be reused in multiple builders
1 parent 3075b55 commit bffaa77

File tree

4 files changed

+25
-49
lines changed

4 files changed

+25
-49
lines changed

‎packages/angular_devkit/build_angular/BUILD.bazel

+1-5
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ ts_library(
228228
":build_angular_test_utils",
229229
"//packages/angular_devkit/architect/testing",
230230
"//packages/angular_devkit/core",
231+
"@npm//fast-glob",
231232
"@npm//prettier",
232233
"@npm//typescript",
233234
"@npm//webpack",
@@ -367,11 +368,6 @@ LARGE_SPECS = {
367368
},
368369
"prerender": {},
369370
"browser-esbuild": {},
370-
"jest": {
371-
"extra_deps": [
372-
"@npm//fast-glob",
373-
],
374-
},
375371
"ssr-dev-server": {
376372
"extra_deps": [
377373
"@npm//@types/browser-sync",

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { execFile as execFileCb } from 'child_process';
1111
import * as path from 'path';
1212
import { promisify } from 'util';
1313
import { colors } from '../../utils/color';
14+
import { findTestFiles } from '../../utils/test-files';
1415
import { buildApplicationInternal } from '../application';
1516
import { ApplicationBuilderInternalOptions } from '../application/options';
1617
import { OutputHashing } from '../browser-esbuild/schema';
1718
import { normalizeOptions } from './options';
1819
import { Schema as JestBuilderSchema } from './schema';
19-
import { findTestFiles } from './test-files';
2020

2121
const execFile = promisify(execFileCb);
2222

@@ -55,7 +55,7 @@ export default createBuilder(
5555
}
5656

5757
// Build all the test files.
58-
const testFiles = await findTestFiles(options, context.workspaceRoot);
58+
const testFiles = await findTestFiles(options.include, options.exclude, context.workspaceRoot);
5959
const jestGlobal = path.join(__dirname, 'jest-global.mjs');
6060
const initTestBed = path.join(__dirname, 'init-test-bed.mjs');
6161
const buildResult = await build(context, {

‎packages/angular_devkit/build_angular/src/builders/jest/test-files.ts renamed to ‎packages/angular_devkit/build_angular/src/utils/test-files.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import fastGlob, { Options as GlobOptions } from 'fast-glob';
10-
import { JestBuilderOptions } from './options';
1110

1211
/**
1312
* Finds all test files in the project.
@@ -19,18 +18,19 @@ import { JestBuilderOptions } from './options';
1918
* @returns A set of all test files in the project.
2019
*/
2120
export async function findTestFiles(
22-
options: JestBuilderOptions,
21+
include: string[],
22+
exclude: string[],
2323
workspaceRoot: string,
2424
glob: typeof fastGlob = fastGlob,
2525
): Promise<Set<string>> {
2626
const globOptions: GlobOptions = {
2727
cwd: workspaceRoot,
28-
ignore: ['node_modules/**'].concat(options.exclude),
28+
ignore: ['node_modules/**'].concat(exclude),
2929
braceExpansion: false, // Do not expand `a{b,c}` to `ab,ac`.
3030
extglob: false, // Disable "extglob" patterns.
3131
};
3232

33-
const included = await Promise.all(options.include.map((pattern) => glob(pattern, globOptions)));
33+
const included = await Promise.all(include.map((pattern) => glob(pattern, globOptions)));
3434

3535
// Flatten and deduplicate any files found in multiple include patterns.
3636
return new Set(included.flat());

‎packages/angular_devkit/build_angular/src/builders/jest/tests/test-files_spec.ts renamed to ‎packages/angular_devkit/build_angular/src/utils/test-files_spec.ts

+18-38
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
import realGlob from 'fast-glob';
1111
import { promises as fs } from 'fs';
1212
import * as path from 'path';
13-
import { findTestFiles } from '../test-files';
14-
import { BASE_OPTIONS } from './options';
13+
import { findTestFiles } from './test-files';
1514

1615
describe('test-files', () => {
1716
describe('findTestFiles()', () => {
@@ -31,11 +30,8 @@ describe('test-files', () => {
3130
await fs.writeFile(path.join(tempDir, 'nested', 'bar.spec.ts'), '');
3231

3332
const testFiles = await findTestFiles(
34-
{
35-
...BASE_OPTIONS,
36-
include: ['**/*.spec.ts'],
37-
exclude: [],
38-
},
33+
['**/*.spec.ts'] /* include */,
34+
[] /* exclude */,
3935
tempDir,
4036
);
4137

@@ -49,11 +45,8 @@ describe('test-files', () => {
4945
await fs.writeFile(path.join(tempDir, 'node_modules', 'dep', 'baz.spec.ts'), '');
5046

5147
const testFiles = await findTestFiles(
52-
{
53-
...BASE_OPTIONS,
54-
include: ['**/*.spec.ts'],
55-
exclude: ['**/*.ignored.spec.ts'],
56-
},
48+
['**/*.spec.ts'] /* include */,
49+
['**/*.ignored.spec.ts'] /* exclude */,
5750
tempDir,
5851
);
5952

@@ -71,12 +64,9 @@ describe('test-files', () => {
7164
await fs.writeFile(path.join(tempDir, 'node_modules', 'dep', 'baz.test.ts'), '');
7265

7366
const testFiles = await findTestFiles(
74-
{
75-
...BASE_OPTIONS,
76-
include: ['**/*.spec.ts', '**/*.test.ts'],
77-
// Exclude should be applied to all `glob()` executions.
78-
exclude: ['**/*.ignored.*.ts'],
79-
},
67+
['**/*.spec.ts', '**/*.test.ts'] /* include */,
68+
// Exclude should be applied to all `glob()` executions.
69+
['**/*.ignored.*.ts'] /* exclude */,
8070
tempDir,
8171
);
8272

@@ -89,10 +79,8 @@ describe('test-files', () => {
8979
await fs.writeFile(path.join(tempDir, 'nested', 'bar.spec.ts'), '');
9080

9181
const testFiles = await findTestFiles(
92-
{
93-
...BASE_OPTIONS,
94-
include: ['**/*.spec.ts'],
95-
},
82+
['**/*.spec.ts'] /* include */,
83+
[] /* exclude */,
9684
path.join(tempDir, 'nested'),
9785
);
9886

@@ -111,10 +99,8 @@ describe('test-files', () => {
11199

112100
await expectAsync(
113101
findTestFiles(
114-
{
115-
...BASE_OPTIONS,
116-
include: ['*.spec.ts', '*.stuff.ts', '*.test.ts'],
117-
},
102+
['*.spec.ts', '*.stuff.ts', '*.test.ts'] /* include */,
103+
[] /* exclude */,
118104
tempDir,
119105
// eslint-disable-next-line @typescript-eslint/no-explicit-any
120106
glob as any,
@@ -127,10 +113,8 @@ describe('test-files', () => {
127113
await fs.writeFile(path.join(tempDir, 'bar.spec.ts'), '');
128114

129115
const testFiles = await findTestFiles(
130-
{
131-
...BASE_OPTIONS,
132-
include: ['{foo,bar}.spec.ts'],
133-
},
116+
['{foo,bar}.spec.ts'] /* include */,
117+
[] /* exclude */,
134118
tempDir,
135119
);
136120

@@ -142,10 +126,8 @@ describe('test-files', () => {
142126
await fs.writeFile(path.join(tempDir, 'bar.spec.ts'), '');
143127

144128
const testFiles = await findTestFiles(
145-
{
146-
...BASE_OPTIONS,
147-
include: ['+(foo|bar).spec.ts'],
148-
},
129+
['+(foo|bar).spec.ts'] /* include */,
130+
[] /* exclude */,
149131
tempDir,
150132
);
151133

@@ -158,10 +140,8 @@ describe('test-files', () => {
158140
await fs.writeFile(path.join(tempDir, 'bar.spec.ts', 'baz.spec.ts'), '');
159141

160142
const testFiles = await findTestFiles(
161-
{
162-
...BASE_OPTIONS,
163-
include: ['**/*.spec.ts'],
164-
},
143+
['**/*.spec.ts'] /* include */,
144+
[] /* exclude */,
165145
tempDir,
166146
);
167147

0 commit comments

Comments
 (0)