Skip to content

Commit 3264c17

Browse files
clydinalan-agius4
authored andcommitted
test(@angular/build): add test harness rebuild case helper function
To reduce duplicate code within tests for rebuild/watch scenarios, a new helper has been added to the builder test harness. This helper allows passing in an array of test case functions that are executed in order per rebuild. The development server's watch option tests have been updated to use the new helper.
1 parent 6c618d4 commit 3264c17

File tree

2 files changed

+71
-68
lines changed

2 files changed

+71
-68
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
import { BuilderHandlerFn } from '@angular-devkit/architect';
1010
import { json } from '@angular-devkit/core';
1111
import { readFileSync } from 'fs';
12-
import { BuilderHarness } from './builder-harness';
12+
import { concatMap, count, firstValueFrom, take, timeout } from 'rxjs';
13+
import { BuilderHarness, BuilderHarnessExecutionResult } from './builder-harness';
1314
import { host } from './test-utils';
1415

16+
/**
17+
* Maximum time for single build/rebuild
18+
* This accounts for CI variability.
19+
*/
20+
export const BUILD_TIMEOUT = 25_000;
21+
1522
const optionSchemaCache = new Map<string, json.schema.JsonSchema>();
1623

1724
export function describeBuilder<T>(
@@ -45,6 +52,24 @@ export class JasmineBuilderHarness<T> extends BuilderHarness<T> {
4552
expectDirectory(path: string): HarnessDirectoryMatchers {
4653
return expectDirectory(path, this);
4754
}
55+
56+
async executeWithCases(
57+
cases: ((
58+
executionResult: BuilderHarnessExecutionResult,
59+
index: number,
60+
) => void | Promise<void>)[],
61+
): Promise<void> {
62+
const executionCount = await firstValueFrom(
63+
this.execute().pipe(
64+
timeout(BUILD_TIMEOUT),
65+
concatMap(async (result, index) => await cases[index](result, index)),
66+
take(cases.length),
67+
count(),
68+
),
69+
);
70+
71+
expect(executionCount).toBe(cases.length);
72+
}
4873
}
4974

5075
export interface HarnessFileMatchers {

packages/angular/build/src/builders/dev-server/tests/options/watch_spec.ts

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

9-
import { TimeoutError, concatMap, count, take, timeout } from 'rxjs';
9+
import { TimeoutError } from 'rxjs';
1010
import { executeDevServer } from '../../index';
1111
import { describeServeBuilder } from '../jasmine-helpers';
12-
import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup';
12+
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';
1313

1414
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
1515
describe('Option: "watch"', () => {
@@ -24,27 +24,21 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
2424
});
2525

2626
await harness
27-
.execute()
28-
.pipe(
29-
timeout(BUILD_TIMEOUT),
30-
concatMap(async ({ result }, index) => {
27+
.executeWithCases([
28+
async ({ result }) => {
29+
// Initial build should succeed
3130
expect(result?.success).toBe(true);
3231

33-
switch (index) {
34-
case 0:
35-
await harness.modifyFile(
36-
'src/main.ts',
37-
(content) => content + 'console.log("abcd1234");',
38-
);
39-
break;
40-
case 1:
41-
fail('Expected files to not be watched.');
42-
break;
43-
}
44-
}),
45-
take(2),
46-
)
47-
.toPromise()
32+
// Modify a file to attempt to trigger file watcher
33+
await harness.modifyFile(
34+
'src/main.ts',
35+
(content) => content + 'console.log("abcd1234");',
36+
);
37+
},
38+
() => {
39+
fail('Expected files to not be watched.');
40+
},
41+
])
4842
.catch((error) => {
4943
// Timeout is expected if watching is disabled
5044
if (error instanceof TimeoutError) {
@@ -60,30 +54,22 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
6054
watch: undefined,
6155
});
6256

63-
const buildCount = await harness
64-
.execute()
65-
.pipe(
66-
timeout(BUILD_TIMEOUT),
67-
concatMap(async ({ result }, index) => {
68-
expect(result?.success).toBe(true);
69-
70-
switch (index) {
71-
case 0:
72-
await harness.modifyFile(
73-
'src/main.ts',
74-
(content) => content + 'console.log("abcd1234");',
75-
);
76-
break;
77-
case 1:
78-
break;
79-
}
80-
}),
81-
take(2),
82-
count(),
83-
)
84-
.toPromise();
57+
await harness.executeWithCases([
58+
async ({ result }) => {
59+
// Initial build should succeed
60+
expect(result?.success).toBe(true);
8561

86-
expect(buildCount).toBe(2);
62+
// Modify a file to trigger file watcher
63+
await harness.modifyFile(
64+
'src/main.ts',
65+
(content) => content + 'console.log("abcd1234");',
66+
);
67+
},
68+
async ({ result }) => {
69+
// Modifying a file should trigger a successful rebuild
70+
expect(result?.success).toBe(true);
71+
},
72+
]);
8773
});
8874

8975
it('watches for file changes when true', async () => {
@@ -92,30 +78,22 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
9278
watch: true,
9379
});
9480

95-
const buildCount = await harness
96-
.execute()
97-
.pipe(
98-
timeout(BUILD_TIMEOUT),
99-
concatMap(async ({ result }, index) => {
100-
expect(result?.success).toBe(true);
101-
102-
switch (index) {
103-
case 0:
104-
await harness.modifyFile(
105-
'src/main.ts',
106-
(content) => content + 'console.log("abcd1234");',
107-
);
108-
break;
109-
case 1:
110-
break;
111-
}
112-
}),
113-
take(2),
114-
count(),
115-
)
116-
.toPromise();
81+
await harness.executeWithCases([
82+
async ({ result }) => {
83+
// Initial build should succeed
84+
expect(result?.success).toBe(true);
11785

118-
expect(buildCount).toBe(2);
86+
// Modify a file to trigger file watcher
87+
await harness.modifyFile(
88+
'src/main.ts',
89+
(content) => content + 'console.log("abcd1234");',
90+
);
91+
},
92+
async ({ result }) => {
93+
// Modifying a file should trigger a successful rebuild
94+
expect(result?.success).toBe(true);
95+
},
96+
]);
11997
});
12098
});
12199
});

0 commit comments

Comments
 (0)