Skip to content

Commit abe9dac

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

22 files changed

+309
-358
lines changed

tests/legacy-cli/e2e/tests/commands/add/add-pwa.ts

Lines changed: 8 additions & 12 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 { expectFileToExist, readFile, rimraf } from '../../../utils/fs';
34
import { installWorkspacePackages } from '../../../utils/packages';
@@ -17,9 +18,7 @@ export default async function () {
1718
const hasPWADep = Object.keys({ ...dependencies, ...devDependencies }).some(
1819
(d) => d === '@angular/pwa',
1920
);
20-
if (hasPWADep) {
21-
throw new Error(`Expected 'package.json' not to contain a dependency on '@angular/pwa'.`);
22-
}
21+
assert.ok(!hasPWADep, `Expected 'package.json' not to contain a dependency on '@angular/pwa'.`);
2322

2423
const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
2524
if (isSnapshotBuild) {
@@ -55,13 +54,10 @@ export default async function () {
5554
}));
5655
const emptyAssetGroups = assetGroups.filter(({ urlCount }) => urlCount === 0);
5756

58-
if (assetGroups.length === 0) {
59-
throw new Error("Expected 'ngsw.json' to contain at least one asset-group.");
60-
}
61-
if (emptyAssetGroups.length > 0) {
62-
throw new Error(
63-
'Expected all asset-groups to contain at least one URL, but the following groups are empty: ' +
64-
emptyAssetGroups.map(({ name }) => name).join(', '),
65-
);
66-
}
57+
assert.ok(assetGroups.length > 0, "Expected 'ngsw.json' to contain at least one asset-group.");
58+
assert.ok(
59+
emptyAssetGroups.length === 0,
60+
'Expected all asset-groups to contain at least one URL, but the following groups are empty: ' +
61+
emptyAssetGroups.map(({ name }) => name).join(', '),
62+
);
6763
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict';
12
import { assetDir } from '../../../utils/assets';
23
import { ng } from '../../../utils/process';
34

@@ -9,21 +10,15 @@ export default async function () {
910
assetDir('add-collection-peer-bad'),
1011
'--skip-confirmation',
1112
);
12-
if (!bad.includes(warning)) {
13-
throw new Error('peer warning not shown on bad package');
14-
}
13+
assert.match(bad, new RegExp(warning), 'peer warning not shown on bad package');
1514

1615
const { stdout: base } = await ng('add', assetDir('add-collection'), '--skip-confirmation');
17-
if (base.includes(warning)) {
18-
throw new Error('peer warning shown on base package');
19-
}
16+
assert.doesNotMatch(base, new RegExp(warning), 'peer warning shown on base package');
2017

2118
const { stdout: good } = await ng(
2219
'add',
2320
assetDir('add-collection-peer-good'),
2421
'--skip-confirmation',
2522
);
26-
if (good.includes(warning)) {
27-
throw new Error('peer warning shown on good package');
28-
}
23+
assert.doesNotMatch(good, new RegExp(warning), 'peer warning shown on good package');
2924
}

tests/legacy-cli/e2e/tests/commands/add/version-specifier.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict';
12
import { appendFile } from 'node:fs/promises';
23
import { expectFileToMatch } from '../../../utils/fs';
34
import { getActivePackageManager, uninstallPackage } from '../../../utils/packages';
@@ -21,24 +22,32 @@ export default async function () {
2122
await expectFileToMatch('package.json', /@angular\/localize/);
2223

2324
const output1 = await ng('add', '@angular/localize', '--skip-confirmation');
24-
if (!output1.stdout.includes('Skipping installation: Package already installed')) {
25-
throw new Error('Installation was not skipped');
26-
}
25+
assert.match(
26+
output1.stdout,
27+
/Skipping installation: Package already installed/,
28+
'Installation was not skipped',
29+
);
2730

2831
const output2 = await ng('add', '@angular/localize@latest', '--skip-confirmation');
29-
if (output2.stdout.includes('Skipping installation: Package already installed')) {
30-
throw new Error('Installation should not have been skipped');
31-
}
32+
assert.doesNotMatch(
33+
output2.stdout,
34+
/Skipping installation: Package already installed/,
35+
'Installation should not have been skipped',
36+
);
3237

3338
const output3 = await ng('add', '@angular/localize@19.1.0', '--skip-confirmation');
34-
if (output3.stdout.includes('Skipping installation: Package already installed')) {
35-
throw new Error('Installation should not have been skipped');
36-
}
39+
assert.doesNotMatch(
40+
output3.stdout,
41+
/Skipping installation: Package already installed/,
42+
'Installation should not have been skipped',
43+
);
3744

3845
const output4 = await ng('add', '@angular/localize@19', '--skip-confirmation');
39-
if (!output4.stdout.includes('Skipping installation: Package already installed')) {
40-
throw new Error('Installation was not skipped');
41-
}
46+
assert.match(
47+
output4.stdout,
48+
/Skipping installation: Package already installed/,
49+
'Installation was not skipped',
50+
);
4251

4352
await uninstallPackage('@angular/localize');
4453
}

tests/legacy-cli/e2e/tests/commands/analytics/ask-analytics-command.ts

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

@@ -17,9 +18,7 @@ export default async function () {
1718
'n\n' /* stdin */,
1819
);
1920

20-
if (!ANALYTICS_PROMPT.test(stdout)) {
21-
throw new Error('CLI did not prompt for analytics permission.');
22-
}
21+
assert.match(stdout, ANALYTICS_PROMPT, 'CLI did not prompt for analytics permission.');
2322
});
2423

2524
// CLI should skip analytics prompt with `NG_CLI_ANALYTICS=false`.
@@ -31,9 +30,11 @@ export default async function () {
3130
NG_FORCE_AUTOCOMPLETE: 'false',
3231
});
3332

34-
if (ANALYTICS_PROMPT.test(stdout)) {
35-
throw new Error('CLI prompted for analytics permission when it should be forced off.');
36-
}
33+
assert.doesNotMatch(
34+
stdout,
35+
ANALYTICS_PROMPT,
36+
'CLI prompted for analytics permission when it should be forced off.',
37+
);
3738
});
3839

3940
// CLI should skip analytics prompt during `ng update`.
@@ -44,10 +45,10 @@ export default async function () {
4445
NG_FORCE_AUTOCOMPLETE: 'false',
4546
});
4647

47-
if (ANALYTICS_PROMPT.test(stdout)) {
48-
throw new Error(
49-
'CLI prompted for analytics permission during an update where it should not' + ' have.',
50-
);
51-
}
48+
assert.doesNotMatch(
49+
stdout,
50+
ANALYTICS_PROMPT,
51+
'CLI prompted for analytics permission during an update where it should not have.',
52+
);
5253
});
5354
}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
import assert from 'node:assert/strict';
12
import { readFile } from '../../../utils/fs';
23
import { ng } from '../../../utils/process';
34

45
export default async function () {
56
await ng('cache', 'enable');
6-
if (JSON.parse(await readFile('angular.json')).cli.cache.enabled !== true) {
7-
throw new Error(`Expected 'cli.cache.enable' to be true.`);
8-
}
7+
assert.strictEqual(
8+
JSON.parse(await readFile('angular.json')).cli.cache.enabled,
9+
true,
10+
`Expected 'cli.cache.enable' to be true.`,
11+
);
912

1013
await ng('cache', 'disable');
11-
if (JSON.parse(await readFile('angular.json')).cli.cache.enabled !== false) {
12-
throw new Error(`Expected 'cli.cache.enable' to be false.`);
13-
}
14+
assert.strictEqual(
15+
JSON.parse(await readFile('angular.json')).cli.cache.enabled,
16+
false,
17+
`Expected 'cli.cache.enable' to be false.`,
18+
);
1419
}

0 commit comments

Comments
 (0)