Skip to content

Commit 4c3a956

Browse files
clydinalan-agius4
authored andcommitted
test: assert catch clause variable type before usage in E2E tests
Prepares the E2E tests for the eventual change of enabling the TypeScript `useUnknownInCatchVariables` option. This option provides additional code safety by ensuring that the catch clause variable is the proper type before attempting to access its properties. Similar changes will be needed in the other packages in the repository prior to enabling `useUnknownInCatchVariables`.
1 parent bb43e0a commit 4c3a956

File tree

7 files changed

+31
-11
lines changed

7 files changed

+31
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default async function () {
1212
try {
1313
await ng('add', `@angular/material${tag}`, '--unknown', '--skip-confirmation');
1414
} catch (error) {
15-
if (!(error.message && error.message.includes(`Unknown option: '--unknown'`))) {
15+
if (!(error instanceof Error && error.message.includes(`Unknown option: '--unknown'`))) {
1616
throw error;
1717
}
1818
}

tests/legacy-cli/e2e/tests/commands/help/help-json.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,21 @@ export default async function () {
5252
try {
5353
JSON.parse(stdout2.trim());
5454
} catch (error) {
55-
throw new Error(`'ng --help ---json-help' failed to return JSON.\n${error.message}`);
55+
throw new Error(
56+
`'ng --help ---json-help' failed to return JSON.\n${
57+
error instanceof Error ? error.message : error
58+
}`,
59+
);
5660
}
5761

5862
const { stdout: stdout3 } = await silentNg('generate', '--help', '--json-help');
5963
try {
6064
JSON.parse(stdout3.trim());
6165
} catch (error) {
62-
throw new Error(`'ng generate --help ---json-help' failed to return JSON.\n${error.message}`);
66+
throw new Error(
67+
`'ng generate --help ---json-help' failed to return JSON.\n${
68+
error instanceof Error ? error.message : error
69+
}`,
70+
);
6371
}
6472
}

tests/legacy-cli/e2e/tests/commands/unknown-configuration.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ export default async function () {
55
await ng('build', '--configuration', 'invalid');
66
throw new Error('should have failed.');
77
} catch (error) {
8-
if (!error.message.includes(`Configuration 'invalid' is not set in the workspace`)) {
8+
if (
9+
!(
10+
error instanceof Error &&
11+
error.message.includes(`Configuration 'invalid' is not set in the workspace`)
12+
)
13+
) {
914
throw error;
1015
}
1116
}

tests/legacy-cli/e2e/tests/misc/http-headers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default async function () {
2828
try {
2929
await ng('e2e');
3030
} catch (error) {
31-
errorMessage = error.message;
31+
errorMessage = error instanceof Error ? error.message : null;
3232
}
3333

3434
if (!errorMessage) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default async function () {
1616
await ng('test', '--no-watch', '--source-map');
1717
throw new Error('ng test should have failed.');
1818
} catch (error) {
19-
if (!error.message.includes('app.component.spec.ts')) {
19+
if (!(error instanceof Error && error.message.includes('app.component.spec.ts'))) {
2020
throw error;
2121
}
2222
}
@@ -26,7 +26,7 @@ export default async function () {
2626
await ng('test', '--no-watch', '--no-source-map');
2727
throw new Error('ng test should have failed.');
2828
} catch (error) {
29-
if (!error.message.includes('main.js')) {
29+
if (!(error instanceof Error && error.message.includes('main.js'))) {
3030
throw error;
3131
}
3232
}

tests/legacy-cli/e2e/utils/process.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ export async function execAndCaptureError(
242242
await _exec({ env, stdin }, cmd, args);
243243
throw new Error('Tried to capture subprocess exception, but it completed successfully.');
244244
} catch (err) {
245-
return err;
245+
if (err instanceof Error) {
246+
return err;
247+
}
248+
throw new Error('Subprocess exception was not an Error instance');
246249
}
247250
}
248251

tests/legacy-cli/e2e_runner.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,13 @@ Promise.all([findFreePort(), findFreePort()])
139139

140140
console.log(colors.green('Done.'));
141141
} catch (err) {
142-
console.log('\n');
143-
console.error(colors.red(err.message));
144-
console.error(colors.red(err.stack));
142+
if (err instanceof Error) {
143+
console.log('\n');
144+
console.error(colors.red(err.message));
145+
if (err.stack) {
146+
console.error(colors.red(err.stack));
147+
}
148+
}
145149

146150
if (argv.debug) {
147151
console.log(`Current Directory: ${process.cwd()}`);

0 commit comments

Comments
 (0)