Skip to content

Commit 87d9c76

Browse files
committed
test: use node:assert in basic and build E2E tests
Replaces error throwing with Node.js assertions in E2E tests. This provides more informative error messages when tests fail.
1 parent ec0dfc9 commit 87d9c76

File tree

11 files changed

+77
-125
lines changed

11 files changed

+77
-125
lines changed

tests/legacy-cli/e2e/tests/basic/rebuild.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict';
12
import { setTimeout } from 'node:timers/promises';
23
import { getGlobalVariable } from '../../utils/env';
34
import { appendToFile, replaceInFile, writeMultipleFiles } from '../../utils/fs';
@@ -68,15 +69,9 @@ export default async function () {
6869
{
6970
const response = await fetch(`http://localhost:${port}/main.js`);
7071
const body = await response.text();
71-
if (!body.match(/\$\$_E2E_GOLDEN_VALUE_1/)) {
72-
throw new Error('Expected golden value 1.');
73-
}
74-
if (!body.match(/\$\$_E2E_GOLDEN_VALUE_2/)) {
75-
throw new Error('Expected golden value 2.');
76-
}
77-
if (!body.match(/\$\$_E2E_GOLDEN_VALUE_3/)) {
78-
throw new Error('Expected golden value 3.');
79-
}
72+
assert.match(body, /\$\$_E2E_GOLDEN_VALUE_1/);
73+
assert.match(body, /\$\$_E2E_GOLDEN_VALUE_2/);
74+
assert.match(body, /\$\$_E2E_GOLDEN_VALUE_3/);
8075
}
8176

8277
await setTimeout(500);
@@ -90,9 +85,7 @@ export default async function () {
9085
{
9186
const response = await fetch(`http://localhost:${port}/main.js`);
9287
const body = await response.text();
93-
if (!body.match(/testingTESTING123/)) {
94-
throw new Error('Expected component HTML to update.');
95-
}
88+
assert.match(body, /testingTESTING123/);
9689
}
9790

9891
await setTimeout(500);
@@ -106,9 +99,7 @@ export default async function () {
10699
{
107100
const response = await fetch(`http://localhost:${port}/main.js`);
108101
const body = await response.text();
109-
if (!body.match(/color:\s?blue/)) {
110-
throw new Error('Expected component CSS to update.');
111-
}
102+
assert.match(body, /color:\s?blue/);
112103
}
113104

114105
await setTimeout(500);
@@ -122,8 +113,6 @@ export default async function () {
122113
{
123114
const response = await fetch(`http://localhost:${port}/styles.css`);
124115
const body = await response.text();
125-
if (!body.match(/color:\s?green/)) {
126-
throw new Error('Expected global CSS to update.');
127-
}
116+
assert.match(body, /color:\s?green/);
128117
}
129118
}

tests/legacy-cli/e2e/tests/basic/serve.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict';
12
import { killAllProcesses } from '../../utils/process';
23
import { ngServe } from '../../utils/project';
34

@@ -14,14 +15,8 @@ export default async function () {
1415

1516
async function verifyResponse(port: number): Promise<void> {
1617
const indexResponse = await fetch(`http://localhost:${port}/`);
17-
18-
if (!/<app-root><\/app-root>/.test(await indexResponse.text())) {
19-
throw new Error('Response does not match expected value.');
20-
}
18+
assert.match(await indexResponse.text(), /<app-root><\/app-root>/);
2119

2220
const assetResponse = await fetch(`http://localhost:${port}/favicon.ico`);
23-
24-
if (!assetResponse.ok) {
25-
throw new Error('Expected favicon asset to be available.');
26-
}
21+
assert(assetResponse.ok, 'Expected favicon asset to be available.');
2722
}

tests/legacy-cli/e2e/tests/basic/styles-array.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict';
12
import { expectFileToMatch, writeMultipleFiles } from '../../utils/fs';
23
import { ng } from '../../utils/process';
34
import { updateJsonFile } from '../../utils/project';
@@ -42,8 +43,5 @@ export default async function () {
4243
);
4344

4445
// Non injected styles should be listed under lazy chunk files
45-
if (!/Lazy chunk files[\s\S]+renamed-lazy-style\.css/m.test(stdout)) {
46-
console.log(stdout);
47-
throw new Error(`Expected "renamed-lazy-style.css" to be listed under "Lazy chunk files".`);
48-
}
46+
assert.match(stdout, /Lazy chunk files[\s\S]+renamed-lazy-style\.css/m);
4947
}

tests/legacy-cli/e2e/tests/build/bundle-budgets.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.dev/license
77
*/
8+
import assert from 'node:assert/strict';
89
import { ng } from '../../utils/process';
910
import { updateJsonFile } from '../../utils/project';
1011
import { expectToFail } from '../../utils/utils';
@@ -18,9 +19,7 @@ export default async function () {
1819
});
1920

2021
const { message: errorMessage } = await expectToFail(() => ng('build'));
21-
if (!/Error.+budget/i.test(errorMessage)) {
22-
throw new Error('Budget error: all, max error.');
23-
}
22+
assert.match(errorMessage, /Error.+budget/i, 'Budget error: all, max error.');
2423

2524
// Warning
2625
await updateJsonFile('angular.json', (json) => {
@@ -30,9 +29,7 @@ export default async function () {
3029
});
3130

3231
const { stderr } = await ng('build');
33-
if (!/Warning.+budget/i.test(stderr)) {
34-
throw new Error('Budget warning: all, min warning');
35-
}
32+
assert.match(stderr, /Warning.+budget/i, 'Budget warning: all, min warning');
3633

3734
// Pass
3835
await updateJsonFile('angular.json', (json) => {
@@ -42,7 +39,5 @@ export default async function () {
4239
});
4340

4441
const { stderr: stderr2 } = await ng('build');
45-
if (/(Warning|Error)/i.test(stderr2)) {
46-
throw new Error('BIG max for all, should not error');
47-
}
42+
assert.doesNotMatch(stderr2, /(Warning|Error)/i, 'BIG max for all, should not error');
4843
}

tests/legacy-cli/e2e/tests/build/prod-build.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict';
12
import { statSync } from 'node:fs';
23
import { join } from 'node:path';
34
import { getGlobalVariable } from '../../utils/env';
@@ -10,17 +11,15 @@ function verifySize(bundle: string, baselineBytes: number) {
1011
const maxSize = baselineBytes + percentageBaseline;
1112
const minSize = baselineBytes - percentageBaseline;
1213

13-
if (size >= maxSize) {
14-
throw new Error(
15-
`Expected ${bundle} size to be less than ${maxSize / 1024}Kb but it was ${size / 1024}Kb.`,
16-
);
17-
}
14+
assert(
15+
size < maxSize,
16+
`Expected ${bundle} size to be less than ${maxSize / 1024}Kb but it was ${size / 1024}Kb.`,
17+
);
1818

19-
if (size <= minSize) {
20-
throw new Error(
21-
`Expected ${bundle} size to be greater than ${minSize / 1024}Kb but it was ${size / 1024}Kb.`,
22-
);
23-
}
19+
assert(
20+
size > minSize,
21+
`Expected ${bundle} size to be greater than ${minSize / 1024}Kb but it was ${size / 1024}Kb.`,
22+
);
2423
}
2524

2625
export default async function () {

tests/legacy-cli/e2e/tests/build/progress-and-stats.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@ import { ng } from '../../utils/process';
44

55
export default async function () {
66
const { stderr: stderrProgress, stdout } = await ng('build', '--progress');
7-
if (!stdout.includes('Initial total')) {
8-
throw new Error(`Expected stdout to contain 'Initial total' but it did not.\n${stdout}`);
9-
}
10-
11-
if (!stdout.includes('Estimated transfer size')) {
12-
throw new Error(
13-
`Expected stdout to contain 'Estimated transfer size' but it did not.\n${stdout}`,
14-
);
15-
}
7+
assert.match(stdout, /Initial total/);
8+
assert.match(stdout, /Estimated transfer size/);
169

1710
let logs;
1811
if (getGlobalVariable('argv')['esbuild']) {
@@ -28,15 +21,11 @@ export default async function () {
2821
}
2922

3023
for (const log of logs) {
31-
if (!stderrProgress.includes(log)) {
32-
throw new Error(`Expected stderr to contain '${log}' but didn't.\n${stderrProgress}`);
33-
}
24+
assert.match(stderrProgress, new RegExp(log));
3425
}
3526

3627
const { stderr: stderrNoProgress } = await ng('build', '--no-progress');
3728
for (const log of logs) {
38-
if (stderrNoProgress.includes(log)) {
39-
throw new Error(`Expected stderr not to contain '${log}' but it did.\n${stderrProgress}`);
40-
}
29+
assert.doesNotMatch(stderrNoProgress, new RegExp(log));
4130
}
4231
}

tests/legacy-cli/e2e/tests/build/rebuild-deps-type-check.ts

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { waitForAnyProcessOutputToMatch, execAndWaitForOutputToMatch } from '../../utils/process';
2-
import { writeFile, prependToFile, appendToFile } from '../../utils/fs';
1+
import assert from 'node:assert/strict';
32
import { getGlobalVariable } from '../../utils/env';
3+
import { appendToFile, prependToFile, writeFile } from '../../utils/fs';
4+
import { execAndWaitForOutputToMatch, waitForAnyProcessOutputToMatch } from '../../utils/process';
45

56
const doneRe = getGlobalVariable('argv')['esbuild']
67
? /Application bundle generation complete\./
@@ -69,14 +70,11 @@ export default function () {
6970
]),
7071
)
7172
.then((results) => {
72-
const stderr = results[0].stderr;
73-
if (
74-
!stderr.includes(
75-
"Argument of type 'string' is not assignable to parameter of type 'number'",
76-
)
77-
) {
78-
throw new Error('Expected an error but none happened.');
79-
}
73+
const { stderr } = results[0];
74+
assert.match(
75+
stderr,
76+
/Argument of type 'string' is not assignable to parameter of type 'number'/,
77+
);
8078
})
8179
// Change an UNRELATED file and the error should still happen.
8280
// Should trigger a rebuild, this time an error is also expected.
@@ -92,14 +90,11 @@ export default function () {
9290
]),
9391
)
9492
.then((results) => {
95-
const stderr = results[0].stderr;
96-
if (
97-
!stderr.includes(
98-
"Argument of type 'string' is not assignable to parameter of type 'number'",
99-
)
100-
) {
101-
throw new Error('Expected an error to still be there but none was.');
102-
}
93+
const { stderr } = results[0];
94+
assert.match(
95+
stderr,
96+
/Argument of type 'string' is not assignable to parameter of type 'number'/,
97+
);
10398
})
10499
// Fix the error!
105100
.then(() =>
@@ -116,14 +111,11 @@ export default function () {
116111
]),
117112
)
118113
.then((results) => {
119-
const stderr = results[0].stderr;
120-
if (
121-
stderr.includes(
122-
"Argument of type 'string' is not assignable to parameter of type 'number'",
123-
)
124-
) {
125-
throw new Error('Expected no error but an error was shown.');
126-
}
114+
const { stderr } = results[0];
115+
assert.doesNotMatch(
116+
stderr,
117+
/Argument of type 'string' is not assignable to parameter of type 'number'/,
118+
);
127119
})
128120
);
129121
}

tests/legacy-cli/e2e/tests/build/relative-sourcemap.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import assert from 'node:assert/strict';
12
import * as fs from 'node:fs';
2-
33
import { isAbsolute } from 'node:path';
44
import { getGlobalVariable } from '../../utils/env';
55
import { ng } from '../../utils/process';
@@ -41,22 +41,17 @@ export default async function () {
4141
const { sources } = JSON.parse(content) as { sources: string[] };
4242
let mainFileFound = false;
4343
for (const source of sources) {
44-
if (isAbsolute(source)) {
45-
throw new Error(`Expected ${source} to be relative.`);
46-
}
44+
assert(!isAbsolute(source), `Expected ${source} to be relative.`);
4745

4846
if (source.endsWith('main.ts')) {
4947
mainFileFound = true;
50-
if (
51-
source !== 'projects/secondary-project/src/main.ts' &&
52-
source !== './projects/secondary-project/src/main.ts'
53-
) {
54-
throw new Error(`Expected main file ${source} to be relative to the workspace root.`);
55-
}
48+
assert(
49+
source === 'projects/secondary-project/src/main.ts' ||
50+
source === './projects/secondary-project/src/main.ts',
51+
`Expected main file ${source} to be relative to the workspace root.`,
52+
);
5653
}
5754
}
5855

59-
if (!mainFileFound) {
60-
throw new Error('Could not find the main file in the application sourcemap sources array.');
61-
}
56+
assert(mainFileFound, 'Could not find the main file in the application sourcemap sources array.');
6257
}

tests/legacy-cli/e2e/tests/build/scripts-output-hashing.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict';
12
import { getGlobalVariable } from '../../utils/env';
23
import {
34
expectFileMatchToExist,
@@ -55,9 +56,10 @@ export default async function () {
5556
`dist/test-project/browser/${filenameBuild2}`,
5657
'try{console.log()}catch{}',
5758
);
58-
if (filenameBuild1 === filenameBuild2) {
59-
throw new Error(
60-
'Contents of the built file changed between builds, but the content hash stayed the same!',
61-
);
62-
}
59+
60+
assert.notEqual(
61+
filenameBuild1,
62+
filenameBuild2,
63+
'Contents of the built file changed between builds, but the content hash stayed the same!',
64+
);
6365
}

tests/legacy-cli/e2e/tests/build/sourcemap.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import assert from 'node:assert/strict';
12
import * as fs from 'node:fs';
3+
import { getGlobalVariable } from '../../utils/env';
24
import { expectFileToExist } from '../../utils/fs';
35
import { ng } from '../../utils/process';
4-
import { getGlobalVariable } from '../../utils/env';
56

67
export default async function () {
78
const useWebpackBuilder = !getGlobalVariable('argv')['esbuild'];
@@ -30,9 +31,7 @@ async function testForSourceMaps(expectedNumberOfFiles: number): Promise<void> {
3031

3132
++count;
3233

33-
if (!files.includes(file + '.map')) {
34-
throw new Error('Sourcemap not generated for ' + file);
35-
}
34+
assert(files.includes(file + '.map'), 'Sourcemap not generated for ' + file);
3635

3736
const content = fs.readFileSync('./dist/test-project/browser/' + file, 'utf8');
3837
let lastLineIndex = content.lastIndexOf('\n');
@@ -41,15 +40,15 @@ async function testForSourceMaps(expectedNumberOfFiles: number): Promise<void> {
4140
lastLineIndex = content.lastIndexOf('\n', lastLineIndex - 1);
4241
}
4342
const comment = lastLineIndex !== -1 && content.slice(lastLineIndex).trim();
44-
if (comment !== `//# sourceMappingURL=${file}.map`) {
45-
console.log('CONTENT:\n' + content);
46-
throw new Error('Sourcemap comment not generated for ' + file);
47-
}
48-
}
49-
50-
if (count < expectedNumberOfFiles) {
51-
throw new Error(
52-
`Javascript file count is low. Expected ${expectedNumberOfFiles} but found ${count}`,
43+
assert.equal(
44+
comment,
45+
`//# sourceMappingURL=${file}.map`,
46+
'Sourcemap comment not generated for ' + file,
5347
);
5448
}
49+
50+
assert(
51+
count >= expectedNumberOfFiles,
52+
`Javascript file count is low. Expected ${expectedNumberOfFiles} but found ${count}`,
53+
);
5554
}

0 commit comments

Comments
 (0)