Skip to content

Commit 3c38877

Browse files
committed
refactor: use executeWithCases instead of RXJS boillerplate
Clean up several tests
1 parent c182286 commit 3c38877

31 files changed

+1212
-1760
lines changed

modules/testing/builder/src/jasmine-helpers.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
import { BuilderHandlerFn } from '@angular-devkit/architect';
1010
import { json } from '@angular-devkit/core';
1111
import { readFileSync } from 'node:fs';
12-
import { concatMap, count, firstValueFrom, take, timeout } from 'rxjs';
13-
import { BuilderHarness, BuilderHarnessExecutionResult } from './builder-harness';
12+
import { concatMap, count, debounceTime, firstValueFrom, take, timeout } from 'rxjs';
13+
import {
14+
BuilderHarness,
15+
BuilderHarnessExecutionOptions,
16+
BuilderHarnessExecutionResult,
17+
} from './builder-harness';
1418
import { host } from './test-utils';
1519

1620
/**
1721
* Maximum time for single build/rebuild
1822
* This accounts for CI variability.
1923
*/
20-
export const BUILD_TIMEOUT = 25_000;
24+
export const BUILD_TIMEOUT = 30_000;
2125

2226
const optionSchemaCache = new Map<string, json.schema.JsonSchema>();
2327

@@ -62,10 +66,12 @@ export class JasmineBuilderHarness<T> extends BuilderHarness<T> {
6266
executionResult: BuilderHarnessExecutionResult,
6367
index: number,
6468
) => void | Promise<void>)[],
69+
options?: Partial<BuilderHarnessExecutionOptions> & { timeout?: number },
6570
): Promise<void> {
6671
const executionCount = await firstValueFrom(
67-
this.execute().pipe(
68-
timeout(BUILD_TIMEOUT),
72+
this.execute(options).pipe(
73+
timeout(options?.timeout ?? BUILD_TIMEOUT),
74+
debounceTime(100), // This is needed as sometimes 2 events for the same change fire with webpack.
6975
concatMap(async (result, index) => await cases[index](result, index)),
7076
take(cases.length),
7177
count(),
@@ -118,13 +124,17 @@ export function expectFile<T>(path: string, harness: BuilderHarness<T>): Harness
118124
return {
119125
toExist() {
120126
const exists = harness.hasFile(path);
121-
expect(exists).toBe(true, 'Expected file to exist: ' + path);
127+
expect(exists)
128+
.withContext('Expected file to exist: ' + path)
129+
.toBeTrue();
122130

123131
return exists;
124132
},
125133
toNotExist() {
126134
const exists = harness.hasFile(path);
127-
expect(exists).toBe(false, 'Expected file to not exist: ' + path);
135+
expect(exists)
136+
.withContext('Expected file to exist: ' + path)
137+
.toBeFalse();
128138

129139
return !exists;
130140
},
@@ -170,13 +180,17 @@ export function expectDirectory<T>(
170180
return {
171181
toExist() {
172182
const exists = harness.hasDirectory(path);
173-
expect(exists).toBe(true, 'Expected directory to exist: ' + path);
183+
expect(exists)
184+
.withContext('Expected directory to exist: ' + path)
185+
.toBeTrue();
174186

175187
return exists;
176188
},
177189
toNotExist() {
178190
const exists = harness.hasDirectory(path);
179-
expect(exists).toBe(false, 'Expected directory to not exist: ' + path);
191+
expect(exists)
192+
.withContext('Expected directory to not exist: ' + path)
193+
.toBeFalse();
180194

181195
return !exists;
182196
},

packages/angular/build/src/builders/application/tests/behavior/rebuild-assets_spec.ts

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { concatMap, count, take, timeout } from 'rxjs';
109
import { buildApplication } from '../../index';
1110
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
1211

13-
/**
14-
* Maximum time in milliseconds for single build/rebuild
15-
* This accounts for CI variability.
16-
*/
17-
const BUILD_TIMEOUT = 10_000;
18-
1912
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
2013
describe('Behavior: "Rebuilds when input asset changes"', () => {
2114
beforeEach(async () => {
@@ -36,30 +29,18 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
3629
watch: true,
3730
});
3831

39-
const buildCount = await harness
40-
.execute({ outputLogsOnFailure: false })
41-
.pipe(
42-
timeout(BUILD_TIMEOUT),
43-
concatMap(async ({ result }, index) => {
44-
switch (index) {
45-
case 0:
46-
expect(result?.success).toBeTrue();
47-
harness.expectFile('dist/browser/asset.txt').content.toContain('foo');
32+
await harness.executeWithCases([
33+
async ({ result }) => {
34+
expect(result?.success).toBeTrue();
35+
harness.expectFile('dist/browser/asset.txt').content.toContain('foo');
4836

49-
await harness.writeFile('public/asset.txt', 'bar');
50-
break;
51-
case 1:
52-
expect(result?.success).toBeTrue();
53-
harness.expectFile('dist/browser/asset.txt').content.toContain('bar');
54-
break;
55-
}
56-
}),
57-
take(2),
58-
count(),
59-
)
60-
.toPromise();
61-
62-
expect(buildCount).toBe(2);
37+
await harness.writeFile('public/asset.txt', 'bar');
38+
},
39+
({ result }) => {
40+
expect(result?.success).toBeTrue();
41+
harness.expectFile('dist/browser/asset.txt').content.toContain('bar');
42+
},
43+
]);
6344
});
6445

6546
it('remove deleted asset from output', async () => {
@@ -79,32 +60,21 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
7960
watch: true,
8061
});
8162

82-
const buildCount = await harness
83-
.execute({ outputLogsOnFailure: false })
84-
.pipe(
85-
timeout(BUILD_TIMEOUT),
86-
concatMap(async ({ result }, index) => {
87-
switch (index) {
88-
case 0:
89-
expect(result?.success).toBeTrue();
90-
harness.expectFile('dist/browser/asset-one.txt').toExist();
91-
harness.expectFile('dist/browser/asset-two.txt').toExist();
63+
await harness.executeWithCases([
64+
async ({ result }) => {
65+
expect(result?.success).toBeTrue();
66+
harness.expectFile('dist/browser/asset-one.txt').toExist();
67+
harness.expectFile('dist/browser/asset-two.txt').toExist();
9268

93-
await harness.removeFile('public/asset-two.txt');
94-
break;
95-
case 1:
96-
expect(result?.success).toBeTrue();
97-
harness.expectFile('dist/browser/asset-one.txt').toExist();
98-
harness.expectFile('dist/browser/asset-two.txt').toNotExist();
99-
break;
100-
}
101-
}),
102-
take(2),
103-
count(),
104-
)
105-
.toPromise();
69+
await harness.removeFile('public/asset-two.txt');
70+
},
10671

107-
expect(buildCount).toBe(2);
72+
({ result }) => {
73+
expect(result?.success).toBeTrue();
74+
harness.expectFile('dist/browser/asset-one.txt').toExist();
75+
harness.expectFile('dist/browser/asset-two.txt').toNotExist();
76+
},
77+
]);
10878
});
10979
});
11080
});

packages/angular/build/src/builders/application/tests/behavior/rebuild-component_styles_spec.ts

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { concatMap, count, take, timeout } from 'rxjs';
109
import { buildApplication } from '../../index';
1110
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
1211

@@ -32,46 +31,31 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
3231
await harness.writeFile('src/app/app.component.scss', "@import './a';");
3332
await harness.writeFile('src/app/a.scss', '$primary: aqua;\\nh1 { color: $primary; }');
3433

35-
const buildCount = await harness
36-
.execute()
37-
.pipe(
38-
timeout(30000),
39-
concatMap(async ({ result }, index) => {
40-
expect(result?.success).toBe(true);
34+
await harness.executeWithCases([
35+
async ({ result }) => {
36+
expect(result?.success).toBe(true);
4137

42-
switch (index) {
43-
case 0:
44-
harness.expectFile('dist/browser/main.js').content.toContain('color: aqua');
45-
harness.expectFile('dist/browser/main.js').content.not.toContain('color: blue');
38+
harness.expectFile('dist/browser/main.js').content.toContain('color: aqua');
39+
harness.expectFile('dist/browser/main.js').content.not.toContain('color: blue');
4640

47-
await harness.writeFile(
48-
'src/app/a.scss',
49-
'$primary: blue;\\nh1 { color: $primary; }',
50-
);
51-
break;
52-
case 1:
53-
harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua');
54-
harness.expectFile('dist/browser/main.js').content.toContain('color: blue');
41+
await harness.writeFile('src/app/a.scss', '$primary: blue;\\nh1 { color: $primary; }');
42+
},
43+
async ({ result }) => {
44+
expect(result?.success).toBe(true);
5545

56-
await harness.writeFile(
57-
'src/app/a.scss',
58-
'$primary: green;\\nh1 { color: $primary; }',
59-
);
60-
break;
61-
case 2:
62-
harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua');
63-
harness.expectFile('dist/browser/main.js').content.not.toContain('color: blue');
64-
harness.expectFile('dist/browser/main.js').content.toContain('color: green');
46+
harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua');
47+
harness.expectFile('dist/browser/main.js').content.toContain('color: blue');
6548

66-
break;
67-
}
68-
}),
69-
take(3),
70-
count(),
71-
)
72-
.toPromise();
49+
await harness.writeFile('src/app/a.scss', '$primary: green;\\nh1 { color: $primary; }');
50+
},
51+
({ result }) => {
52+
expect(result?.success).toBe(true);
7353

74-
expect(buildCount).toBe(3);
54+
harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua');
55+
harness.expectFile('dist/browser/main.js').content.not.toContain('color: blue');
56+
harness.expectFile('dist/browser/main.js').content.toContain('color: green');
57+
},
58+
]);
7559
});
7660
}
7761
});

0 commit comments

Comments
 (0)