diff --git a/tests/legacy-cli/e2e/tests/basic/rebuild.ts b/tests/legacy-cli/e2e/tests/basic/rebuild.ts index d7c75cce9fc5..a0b0f1ddc79d 100644 --- a/tests/legacy-cli/e2e/tests/basic/rebuild.ts +++ b/tests/legacy-cli/e2e/tests/basic/rebuild.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { setTimeout } from 'node:timers/promises'; import { getGlobalVariable } from '../../utils/env'; import { appendToFile, replaceInFile, writeMultipleFiles } from '../../utils/fs'; @@ -68,15 +69,9 @@ export default async function () { { const response = await fetch(`http://localhost:${port}/main.js`); const body = await response.text(); - if (!body.match(/\$\$_E2E_GOLDEN_VALUE_1/)) { - throw new Error('Expected golden value 1.'); - } - if (!body.match(/\$\$_E2E_GOLDEN_VALUE_2/)) { - throw new Error('Expected golden value 2.'); - } - if (!body.match(/\$\$_E2E_GOLDEN_VALUE_3/)) { - throw new Error('Expected golden value 3.'); - } + assert.match(body, /\$\$_E2E_GOLDEN_VALUE_1/); + assert.match(body, /\$\$_E2E_GOLDEN_VALUE_2/); + assert.match(body, /\$\$_E2E_GOLDEN_VALUE_3/); } await setTimeout(500); @@ -90,9 +85,7 @@ export default async function () { { const response = await fetch(`http://localhost:${port}/main.js`); const body = await response.text(); - if (!body.match(/testingTESTING123/)) { - throw new Error('Expected component HTML to update.'); - } + assert.match(body, /testingTESTING123/); } await setTimeout(500); @@ -106,9 +99,7 @@ export default async function () { { const response = await fetch(`http://localhost:${port}/main.js`); const body = await response.text(); - if (!body.match(/color:\s?blue/)) { - throw new Error('Expected component CSS to update.'); - } + assert.match(body, /color:\s?blue/); } await setTimeout(500); @@ -122,8 +113,6 @@ export default async function () { { const response = await fetch(`http://localhost:${port}/styles.css`); const body = await response.text(); - if (!body.match(/color:\s?green/)) { - throw new Error('Expected global CSS to update.'); - } + assert.match(body, /color:\s?green/); } } diff --git a/tests/legacy-cli/e2e/tests/basic/serve.ts b/tests/legacy-cli/e2e/tests/basic/serve.ts index e5f5af674475..7623cc3a6afc 100644 --- a/tests/legacy-cli/e2e/tests/basic/serve.ts +++ b/tests/legacy-cli/e2e/tests/basic/serve.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { killAllProcesses } from '../../utils/process'; import { ngServe } from '../../utils/project'; @@ -14,14 +15,8 @@ export default async function () { async function verifyResponse(port: number): Promise { const indexResponse = await fetch(`http://localhost:${port}/`); - - if (!/<\/app-root>/.test(await indexResponse.text())) { - throw new Error('Response does not match expected value.'); - } + assert.match(await indexResponse.text(), /<\/app-root>/); const assetResponse = await fetch(`http://localhost:${port}/favicon.ico`); - - if (!assetResponse.ok) { - throw new Error('Expected favicon asset to be available.'); - } + assert(assetResponse.ok, 'Expected favicon asset to be available.'); } diff --git a/tests/legacy-cli/e2e/tests/basic/styles-array.ts b/tests/legacy-cli/e2e/tests/basic/styles-array.ts index 1639f8863aac..cc4d6d56f506 100644 --- a/tests/legacy-cli/e2e/tests/basic/styles-array.ts +++ b/tests/legacy-cli/e2e/tests/basic/styles-array.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { expectFileToMatch, writeMultipleFiles } from '../../utils/fs'; import { ng } from '../../utils/process'; import { updateJsonFile } from '../../utils/project'; @@ -42,8 +43,5 @@ export default async function () { ); // Non injected styles should be listed under lazy chunk files - if (!/Lazy chunk files[\s\S]+renamed-lazy-style\.css/m.test(stdout)) { - console.log(stdout); - throw new Error(`Expected "renamed-lazy-style.css" to be listed under "Lazy chunk files".`); - } + assert.match(stdout, /Lazy chunk files[\s\S]+renamed-lazy-style\.css/m); } diff --git a/tests/legacy-cli/e2e/tests/build/bundle-budgets.ts b/tests/legacy-cli/e2e/tests/build/bundle-budgets.ts index d5cbf7059873..1401d8d3c6e3 100644 --- a/tests/legacy-cli/e2e/tests/build/bundle-budgets.ts +++ b/tests/legacy-cli/e2e/tests/build/bundle-budgets.ts @@ -5,6 +5,7 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ +import assert from 'node:assert/strict'; import { ng } from '../../utils/process'; import { updateJsonFile } from '../../utils/project'; import { expectToFail } from '../../utils/utils'; @@ -18,9 +19,7 @@ export default async function () { }); const { message: errorMessage } = await expectToFail(() => ng('build')); - if (!/Error.+budget/i.test(errorMessage)) { - throw new Error('Budget error: all, max error.'); - } + assert.match(errorMessage, /Error.+budget/i, 'Budget error: all, max error.'); // Warning await updateJsonFile('angular.json', (json) => { @@ -30,9 +29,7 @@ export default async function () { }); const { stderr } = await ng('build'); - if (!/Warning.+budget/i.test(stderr)) { - throw new Error('Budget warning: all, min warning'); - } + assert.match(stderr, /Warning.+budget/i, 'Budget warning: all, min warning'); // Pass await updateJsonFile('angular.json', (json) => { @@ -42,7 +39,5 @@ export default async function () { }); const { stderr: stderr2 } = await ng('build'); - if (/(Warning|Error)/i.test(stderr2)) { - throw new Error('BIG max for all, should not error'); - } + assert.doesNotMatch(stderr2, /(Warning|Error)/i, 'BIG max for all, should not error'); } diff --git a/tests/legacy-cli/e2e/tests/build/prod-build.ts b/tests/legacy-cli/e2e/tests/build/prod-build.ts index 3c450aa8d98d..dee45876e379 100644 --- a/tests/legacy-cli/e2e/tests/build/prod-build.ts +++ b/tests/legacy-cli/e2e/tests/build/prod-build.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { statSync } from 'node:fs'; import { join } from 'node:path'; import { getGlobalVariable } from '../../utils/env'; @@ -10,17 +11,15 @@ function verifySize(bundle: string, baselineBytes: number) { const maxSize = baselineBytes + percentageBaseline; const minSize = baselineBytes - percentageBaseline; - if (size >= maxSize) { - throw new Error( - `Expected ${bundle} size to be less than ${maxSize / 1024}Kb but it was ${size / 1024}Kb.`, - ); - } + assert( + size < maxSize, + `Expected ${bundle} size to be less than ${maxSize / 1024}Kb but it was ${size / 1024}Kb.`, + ); - if (size <= minSize) { - throw new Error( - `Expected ${bundle} size to be greater than ${minSize / 1024}Kb but it was ${size / 1024}Kb.`, - ); - } + assert( + size > minSize, + `Expected ${bundle} size to be greater than ${minSize / 1024}Kb but it was ${size / 1024}Kb.`, + ); } export default async function () { diff --git a/tests/legacy-cli/e2e/tests/build/progress-and-stats.ts b/tests/legacy-cli/e2e/tests/build/progress-and-stats.ts index d16c671872fb..940179df052e 100644 --- a/tests/legacy-cli/e2e/tests/build/progress-and-stats.ts +++ b/tests/legacy-cli/e2e/tests/build/progress-and-stats.ts @@ -4,15 +4,8 @@ import { ng } from '../../utils/process'; export default async function () { const { stderr: stderrProgress, stdout } = await ng('build', '--progress'); - if (!stdout.includes('Initial total')) { - throw new Error(`Expected stdout to contain 'Initial total' but it did not.\n${stdout}`); - } - - if (!stdout.includes('Estimated transfer size')) { - throw new Error( - `Expected stdout to contain 'Estimated transfer size' but it did not.\n${stdout}`, - ); - } + assert.match(stdout, /Initial total/); + assert.match(stdout, /Estimated transfer size/); let logs; if (getGlobalVariable('argv')['esbuild']) { @@ -28,15 +21,11 @@ export default async function () { } for (const log of logs) { - if (!stderrProgress.includes(log)) { - throw new Error(`Expected stderr to contain '${log}' but didn't.\n${stderrProgress}`); - } + assert.match(stderrProgress, new RegExp(log)); } const { stderr: stderrNoProgress } = await ng('build', '--no-progress'); for (const log of logs) { - if (stderrNoProgress.includes(log)) { - throw new Error(`Expected stderr not to contain '${log}' but it did.\n${stderrProgress}`); - } + assert.doesNotMatch(stderrNoProgress, new RegExp(log)); } } diff --git a/tests/legacy-cli/e2e/tests/build/rebuild-deps-type-check.ts b/tests/legacy-cli/e2e/tests/build/rebuild-deps-type-check.ts index e390563fda9d..1f4964f6689b 100644 --- a/tests/legacy-cli/e2e/tests/build/rebuild-deps-type-check.ts +++ b/tests/legacy-cli/e2e/tests/build/rebuild-deps-type-check.ts @@ -1,6 +1,7 @@ -import { waitForAnyProcessOutputToMatch, execAndWaitForOutputToMatch } from '../../utils/process'; -import { writeFile, prependToFile, appendToFile } from '../../utils/fs'; +import assert from 'node:assert/strict'; import { getGlobalVariable } from '../../utils/env'; +import { appendToFile, prependToFile, writeFile } from '../../utils/fs'; +import { execAndWaitForOutputToMatch, waitForAnyProcessOutputToMatch } from '../../utils/process'; const doneRe = getGlobalVariable('argv')['esbuild'] ? /Application bundle generation complete\./ @@ -69,14 +70,11 @@ export default function () { ]), ) .then((results) => { - const stderr = results[0].stderr; - if ( - !stderr.includes( - "Argument of type 'string' is not assignable to parameter of type 'number'", - ) - ) { - throw new Error('Expected an error but none happened.'); - } + const { stderr } = results[0]; + assert.match( + stderr, + /Argument of type 'string' is not assignable to parameter of type 'number'/, + ); }) // Change an UNRELATED file and the error should still happen. // Should trigger a rebuild, this time an error is also expected. @@ -92,14 +90,11 @@ export default function () { ]), ) .then((results) => { - const stderr = results[0].stderr; - if ( - !stderr.includes( - "Argument of type 'string' is not assignable to parameter of type 'number'", - ) - ) { - throw new Error('Expected an error to still be there but none was.'); - } + const { stderr } = results[0]; + assert.match( + stderr, + /Argument of type 'string' is not assignable to parameter of type 'number'/, + ); }) // Fix the error! .then(() => @@ -116,14 +111,11 @@ export default function () { ]), ) .then((results) => { - const stderr = results[0].stderr; - if ( - stderr.includes( - "Argument of type 'string' is not assignable to parameter of type 'number'", - ) - ) { - throw new Error('Expected no error but an error was shown.'); - } + const { stderr } = results[0]; + assert.doesNotMatch( + stderr, + /Argument of type 'string' is not assignable to parameter of type 'number'/, + ); }) ); } diff --git a/tests/legacy-cli/e2e/tests/build/relative-sourcemap.ts b/tests/legacy-cli/e2e/tests/build/relative-sourcemap.ts index 11b2cccd082b..209e29aabd76 100644 --- a/tests/legacy-cli/e2e/tests/build/relative-sourcemap.ts +++ b/tests/legacy-cli/e2e/tests/build/relative-sourcemap.ts @@ -1,5 +1,5 @@ +import assert from 'node:assert/strict'; import * as fs from 'node:fs'; - import { isAbsolute } from 'node:path'; import { getGlobalVariable } from '../../utils/env'; import { ng } from '../../utils/process'; @@ -41,22 +41,17 @@ export default async function () { const { sources } = JSON.parse(content) as { sources: string[] }; let mainFileFound = false; for (const source of sources) { - if (isAbsolute(source)) { - throw new Error(`Expected ${source} to be relative.`); - } + assert(!isAbsolute(source), `Expected ${source} to be relative.`); if (source.endsWith('main.ts')) { mainFileFound = true; - if ( - source !== 'projects/secondary-project/src/main.ts' && - source !== './projects/secondary-project/src/main.ts' - ) { - throw new Error(`Expected main file ${source} to be relative to the workspace root.`); - } + assert( + source === 'projects/secondary-project/src/main.ts' || + source === './projects/secondary-project/src/main.ts', + `Expected main file ${source} to be relative to the workspace root.`, + ); } } - if (!mainFileFound) { - throw new Error('Could not find the main file in the application sourcemap sources array.'); - } + assert(mainFileFound, 'Could not find the main file in the application sourcemap sources array.'); } diff --git a/tests/legacy-cli/e2e/tests/build/scripts-output-hashing.ts b/tests/legacy-cli/e2e/tests/build/scripts-output-hashing.ts index 517e185d4af0..8b34662c485e 100644 --- a/tests/legacy-cli/e2e/tests/build/scripts-output-hashing.ts +++ b/tests/legacy-cli/e2e/tests/build/scripts-output-hashing.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { getGlobalVariable } from '../../utils/env'; import { expectFileMatchToExist, @@ -55,9 +56,10 @@ export default async function () { `dist/test-project/browser/${filenameBuild2}`, 'try{console.log()}catch{}', ); - if (filenameBuild1 === filenameBuild2) { - throw new Error( - 'Contents of the built file changed between builds, but the content hash stayed the same!', - ); - } + + assert.notEqual( + filenameBuild1, + filenameBuild2, + 'Contents of the built file changed between builds, but the content hash stayed the same!', + ); } diff --git a/tests/legacy-cli/e2e/tests/build/sourcemap.ts b/tests/legacy-cli/e2e/tests/build/sourcemap.ts index b601ac08656d..cd3d6c32ab0c 100644 --- a/tests/legacy-cli/e2e/tests/build/sourcemap.ts +++ b/tests/legacy-cli/e2e/tests/build/sourcemap.ts @@ -1,7 +1,8 @@ +import assert from 'node:assert/strict'; import * as fs from 'node:fs'; +import { getGlobalVariable } from '../../utils/env'; import { expectFileToExist } from '../../utils/fs'; import { ng } from '../../utils/process'; -import { getGlobalVariable } from '../../utils/env'; export default async function () { const useWebpackBuilder = !getGlobalVariable('argv')['esbuild']; @@ -30,9 +31,7 @@ async function testForSourceMaps(expectedNumberOfFiles: number): Promise { ++count; - if (!files.includes(file + '.map')) { - throw new Error('Sourcemap not generated for ' + file); - } + assert(files.includes(file + '.map'), 'Sourcemap not generated for ' + file); const content = fs.readFileSync('./dist/test-project/browser/' + file, 'utf8'); let lastLineIndex = content.lastIndexOf('\n'); @@ -41,15 +40,15 @@ async function testForSourceMaps(expectedNumberOfFiles: number): Promise { lastLineIndex = content.lastIndexOf('\n', lastLineIndex - 1); } const comment = lastLineIndex !== -1 && content.slice(lastLineIndex).trim(); - if (comment !== `//# sourceMappingURL=${file}.map`) { - console.log('CONTENT:\n' + content); - throw new Error('Sourcemap comment not generated for ' + file); - } - } - - if (count < expectedNumberOfFiles) { - throw new Error( - `Javascript file count is low. Expected ${expectedNumberOfFiles} but found ${count}`, + assert.equal( + comment, + `//# sourceMappingURL=${file}.map`, + 'Sourcemap comment not generated for ' + file, ); } + + assert( + count >= expectedNumberOfFiles, + `Javascript file count is low. Expected ${expectedNumberOfFiles} but found ${count}`, + ); } diff --git a/tests/legacy-cli/e2e/tests/build/worker.ts b/tests/legacy-cli/e2e/tests/build/worker.ts index 7b6cdb390cb7..53e60aa34772 100644 --- a/tests/legacy-cli/e2e/tests/build/worker.ts +++ b/tests/legacy-cli/e2e/tests/build/worker.ts @@ -6,10 +6,11 @@ * found in the LICENSE file at https://angular.dev/license */ +import assert from 'node:assert/strict'; import { readdir } from 'node:fs/promises'; +import { getGlobalVariable } from '../../utils/env'; import { expectFileToExist, expectFileToMatch, replaceInFile, writeFile } from '../../utils/fs'; import { ng } from '../../utils/process'; -import { getGlobalVariable } from '../../utils/env'; import { expectToFail } from '../../utils/utils'; export default async function () { @@ -83,9 +84,7 @@ async function getWorkerOutputFile(useWebpackBuilder: boolean): Promise fileName = files.find((f) => /worker-[\dA-Z]{8}\.js/.test(f)); } - if (!fileName) { - throw new Error('Cannot determine worker output file name.'); - } + assert(fileName, 'Cannot determine worker output file name.'); return fileName; } diff --git a/tests/legacy-cli/e2e/tests/commands/add/add-pwa.ts b/tests/legacy-cli/e2e/tests/commands/add/add-pwa.ts index e4c3be19cc88..c01cc185c84b 100644 --- a/tests/legacy-cli/e2e/tests/commands/add/add-pwa.ts +++ b/tests/legacy-cli/e2e/tests/commands/add/add-pwa.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { getGlobalVariable } from '../../../utils/env'; import { expectFileToExist, readFile, rimraf } from '../../../utils/fs'; import { installWorkspacePackages } from '../../../utils/packages'; @@ -17,9 +18,7 @@ export default async function () { const hasPWADep = Object.keys({ ...dependencies, ...devDependencies }).some( (d) => d === '@angular/pwa', ); - if (hasPWADep) { - throw new Error(`Expected 'package.json' not to contain a dependency on '@angular/pwa'.`); - } + assert.ok(!hasPWADep, `Expected 'package.json' not to contain a dependency on '@angular/pwa'.`); const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots']; if (isSnapshotBuild) { @@ -55,13 +54,10 @@ export default async function () { })); const emptyAssetGroups = assetGroups.filter(({ urlCount }) => urlCount === 0); - if (assetGroups.length === 0) { - throw new Error("Expected 'ngsw.json' to contain at least one asset-group."); - } - if (emptyAssetGroups.length > 0) { - throw new Error( - 'Expected all asset-groups to contain at least one URL, but the following groups are empty: ' + - emptyAssetGroups.map(({ name }) => name).join(', '), - ); - } + assert.ok(assetGroups.length > 0, "Expected 'ngsw.json' to contain at least one asset-group."); + assert.ok( + emptyAssetGroups.length === 0, + 'Expected all asset-groups to contain at least one URL, but the following groups are empty: ' + + emptyAssetGroups.map(({ name }) => name).join(', '), + ); } diff --git a/tests/legacy-cli/e2e/tests/commands/add/peer.ts b/tests/legacy-cli/e2e/tests/commands/add/peer.ts index 85074646de11..d085efadf080 100644 --- a/tests/legacy-cli/e2e/tests/commands/add/peer.ts +++ b/tests/legacy-cli/e2e/tests/commands/add/peer.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { assetDir } from '../../../utils/assets'; import { ng } from '../../../utils/process'; @@ -9,21 +10,15 @@ export default async function () { assetDir('add-collection-peer-bad'), '--skip-confirmation', ); - if (!bad.includes(warning)) { - throw new Error('peer warning not shown on bad package'); - } + assert.match(bad, new RegExp(warning), 'peer warning not shown on bad package'); const { stdout: base } = await ng('add', assetDir('add-collection'), '--skip-confirmation'); - if (base.includes(warning)) { - throw new Error('peer warning shown on base package'); - } + assert.doesNotMatch(base, new RegExp(warning), 'peer warning shown on base package'); const { stdout: good } = await ng( 'add', assetDir('add-collection-peer-good'), '--skip-confirmation', ); - if (good.includes(warning)) { - throw new Error('peer warning shown on good package'); - } + assert.doesNotMatch(good, new RegExp(warning), 'peer warning shown on good package'); } diff --git a/tests/legacy-cli/e2e/tests/commands/add/version-specifier.ts b/tests/legacy-cli/e2e/tests/commands/add/version-specifier.ts index 7bd09835ee9c..f88d60a51ec9 100644 --- a/tests/legacy-cli/e2e/tests/commands/add/version-specifier.ts +++ b/tests/legacy-cli/e2e/tests/commands/add/version-specifier.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { appendFile } from 'node:fs/promises'; import { expectFileToMatch } from '../../../utils/fs'; import { getActivePackageManager, uninstallPackage } from '../../../utils/packages'; @@ -21,24 +22,32 @@ export default async function () { await expectFileToMatch('package.json', /@angular\/localize/); const output1 = await ng('add', '@angular/localize', '--skip-confirmation'); - if (!output1.stdout.includes('Skipping installation: Package already installed')) { - throw new Error('Installation was not skipped'); - } + assert.match( + output1.stdout, + /Skipping installation: Package already installed/, + 'Installation was not skipped', + ); const output2 = await ng('add', '@angular/localize@latest', '--skip-confirmation'); - if (output2.stdout.includes('Skipping installation: Package already installed')) { - throw new Error('Installation should not have been skipped'); - } + assert.doesNotMatch( + output2.stdout, + /Skipping installation: Package already installed/, + 'Installation should not have been skipped', + ); const output3 = await ng('add', '@angular/localize@19.1.0', '--skip-confirmation'); - if (output3.stdout.includes('Skipping installation: Package already installed')) { - throw new Error('Installation should not have been skipped'); - } + assert.doesNotMatch( + output3.stdout, + /Skipping installation: Package already installed/, + 'Installation should not have been skipped', + ); const output4 = await ng('add', '@angular/localize@19', '--skip-confirmation'); - if (!output4.stdout.includes('Skipping installation: Package already installed')) { - throw new Error('Installation was not skipped'); - } + assert.match( + output4.stdout, + /Skipping installation: Package already installed/, + 'Installation was not skipped', + ); await uninstallPackage('@angular/localize'); } diff --git a/tests/legacy-cli/e2e/tests/commands/analytics/ask-analytics-command.ts b/tests/legacy-cli/e2e/tests/commands/analytics/ask-analytics-command.ts index 3791d6850a8d..ae20bb21f2fa 100644 --- a/tests/legacy-cli/e2e/tests/commands/analytics/ask-analytics-command.ts +++ b/tests/legacy-cli/e2e/tests/commands/analytics/ask-analytics-command.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { execWithEnv } from '../../../utils/process'; import { mockHome } from '../../../utils/utils'; @@ -17,9 +18,7 @@ export default async function () { 'n\n' /* stdin */, ); - if (!ANALYTICS_PROMPT.test(stdout)) { - throw new Error('CLI did not prompt for analytics permission.'); - } + assert.match(stdout, ANALYTICS_PROMPT, 'CLI did not prompt for analytics permission.'); }); // CLI should skip analytics prompt with `NG_CLI_ANALYTICS=false`. @@ -31,9 +30,11 @@ export default async function () { NG_FORCE_AUTOCOMPLETE: 'false', }); - if (ANALYTICS_PROMPT.test(stdout)) { - throw new Error('CLI prompted for analytics permission when it should be forced off.'); - } + assert.doesNotMatch( + stdout, + ANALYTICS_PROMPT, + 'CLI prompted for analytics permission when it should be forced off.', + ); }); // CLI should skip analytics prompt during `ng update`. @@ -44,10 +45,10 @@ export default async function () { NG_FORCE_AUTOCOMPLETE: 'false', }); - if (ANALYTICS_PROMPT.test(stdout)) { - throw new Error( - 'CLI prompted for analytics permission during an update where it should not' + ' have.', - ); - } + assert.doesNotMatch( + stdout, + ANALYTICS_PROMPT, + 'CLI prompted for analytics permission during an update where it should not have.', + ); }); } diff --git a/tests/legacy-cli/e2e/tests/commands/cache/cache-enable-disable.ts b/tests/legacy-cli/e2e/tests/commands/cache/cache-enable-disable.ts index bb81dd2d80b9..1cfbf705787e 100644 --- a/tests/legacy-cli/e2e/tests/commands/cache/cache-enable-disable.ts +++ b/tests/legacy-cli/e2e/tests/commands/cache/cache-enable-disable.ts @@ -1,14 +1,19 @@ +import assert from 'node:assert/strict'; import { readFile } from '../../../utils/fs'; import { ng } from '../../../utils/process'; export default async function () { await ng('cache', 'enable'); - if (JSON.parse(await readFile('angular.json')).cli.cache.enabled !== true) { - throw new Error(`Expected 'cli.cache.enable' to be true.`); - } + assert.strictEqual( + JSON.parse(await readFile('angular.json')).cli.cache.enabled, + true, + `Expected 'cli.cache.enable' to be true.`, + ); await ng('cache', 'disable'); - if (JSON.parse(await readFile('angular.json')).cli.cache.enabled !== false) { - throw new Error(`Expected 'cli.cache.enable' to be false.`); - } + assert.strictEqual( + JSON.parse(await readFile('angular.json')).cli.cache.enabled, + false, + `Expected 'cli.cache.enable' to be false.`, + ); } diff --git a/tests/legacy-cli/e2e/tests/commands/completion/completion-prompt.ts b/tests/legacy-cli/e2e/tests/commands/completion/completion-prompt.ts index 3e0a182c2270..424b373e47a6 100644 --- a/tests/legacy-cli/e2e/tests/commands/completion/completion-prompt.ts +++ b/tests/legacy-cli/e2e/tests/commands/completion/completion-prompt.ts @@ -3,6 +3,7 @@ import * as path from 'node:path'; import { env } from 'node:process'; import { getGlobalVariable } from '../../../utils/env'; import { mockHome } from '../../../utils/utils'; +import assert from 'node:assert/strict'; import { execAndCaptureError, @@ -51,20 +52,24 @@ export default async function () { 'y\n' /* stdin: accept prompt */, ); - if (!AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error('CLI execution did not prompt for autocompletion setup when it should have.'); - } + assert.match( + stdout, + AUTOCOMPLETION_PROMPT, + 'CLI execution did not prompt for autocompletion setup when it should have.', + ); const bashrcContents = await fs.readFile(bashrc, 'utf-8'); - if (!bashrcContents.includes('source <(ng completion script)')) { - throw new Error( - 'Autocompletion was *not* added to `~/.bashrc` after accepting the setup prompt.', - ); - } + assert.match( + bashrcContents, + /source <\(ng completion script\)/, + 'Autocompletion was *not* added to `~/.bashrc` after accepting the setup prompt.', + ); - if (!stdout.includes('Appended `source <(ng completion script)`')) { - throw new Error('CLI did not print that it successfully set up autocompletion.'); - } + assert.match( + stdout, + /Appended `source <\(ng completion script\)`/, + 'CLI did not print that it successfully set up autocompletion.', + ); }); // Does nothing if the user rejects the autocompletion prompt. @@ -83,26 +88,30 @@ export default async function () { 'n\n' /* stdin: reject prompt */, ); - if (!AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error('CLI execution did not prompt for autocompletion setup when it should have.'); - } + assert.match( + stdout, + AUTOCOMPLETION_PROMPT, + 'CLI execution did not prompt for autocompletion setup when it should have.', + ); const bashrcContents = await fs.readFile(bashrc, 'utf-8'); - if (bashrcContents.includes('ng completion')) { - throw new Error( - 'Autocompletion was incorrectly added to `~/.bashrc` after refusing the setup prompt.', - ); - } + assert.doesNotMatch( + bashrcContents, + /ng completion/, + 'Autocompletion was incorrectly added to `~/.bashrc` after refusing the setup prompt.', + ); - if (stdout.includes('Appended `source <(ng completion script)`')) { - throw new Error( - "CLI printed that it successfully set up autocompletion when it actually didn't.", - ); - } + assert.doesNotMatch( + stdout, + /Appended `source <\(ng completion script\)`/, + "CLI printed that it successfully set up autocompletion when it actually didn't.", + ); - if (!stdout.includes("Ok, you won't be prompted again.")) { - throw new Error('CLI did not inform the user they will not be prompted again.'); - } + assert.match( + stdout, + /Ok, you won't be prompted again\./, + 'CLI did not inform the user they will not be prompted again.', + ); }); // Does *not* prompt if the user already accepted (even if they delete the completion config). @@ -121,17 +130,19 @@ export default async function () { 'y\n' /* stdin: accept prompt */, ); - if (!AUTOCOMPLETION_PROMPT.test(stdout1)) { - throw new Error('First execution did not prompt for autocompletion setup.'); - } + assert.match( + stdout1, + AUTOCOMPLETION_PROMPT, + 'First execution did not prompt for autocompletion setup.', + ); const bashrcContents1 = await fs.readFile(bashrc, 'utf-8'); - if (!bashrcContents1.includes('source <(ng completion script)')) { - throw new Error( - '`~/.bashrc` file was not updated after the user accepted the autocompletion' + - ` prompt. Contents:\n${bashrcContents1}`, - ); - } + assert.match( + bashrcContents1, + /source <\(ng completion script\)/, + '`~/.bashrc` file was not updated after the user accepted the autocompletion' + + ` prompt. Contents:\n${bashrcContents1}`, + ); // User modifies their configuration and removes `ng completion`. await fs.writeFile(bashrc, '# Some new commands...'); @@ -142,20 +153,20 @@ export default async function () { HOME: home, }); - if (AUTOCOMPLETION_PROMPT.test(stdout2)) { - throw new Error( - 'Subsequent execution after rejecting autocompletion setup prompted again' + - ' when it should not have.', - ); - } + assert.doesNotMatch( + stdout2, + AUTOCOMPLETION_PROMPT, + 'Subsequent execution after rejecting autocompletion setup prompted again' + + ' when it should not have.', + ); const bashrcContents2 = await fs.readFile(bashrc, 'utf-8'); - if (bashrcContents2 !== '# Some new commands...') { - throw new Error( - '`~/.bashrc` file was incorrectly modified when using a modified `~/.bashrc`' + - ` after previously accepting the autocompletion prompt. Contents:\n${bashrcContents2}`, - ); - } + assert.strictEqual( + bashrcContents2, + '# Some new commands...', + '`~/.bashrc` file was incorrectly modified when using a modified `~/.bashrc`' + + ` after previously accepting the autocompletion prompt. Contents:\n${bashrcContents2}`, + ); }); // Does *not* prompt if the user already rejected. @@ -174,9 +185,11 @@ export default async function () { 'n\n' /* stdin: reject prompt */, ); - if (!AUTOCOMPLETION_PROMPT.test(stdout1)) { - throw new Error('First execution did not prompt for autocompletion setup.'); - } + assert.match( + stdout1, + AUTOCOMPLETION_PROMPT, + 'First execution did not prompt for autocompletion setup.', + ); const { stdout: stdout2 } = await execWithEnv('ng', ['config'], { ...DEFAULT_ENV, @@ -184,20 +197,20 @@ export default async function () { HOME: home, }); - if (AUTOCOMPLETION_PROMPT.test(stdout2)) { - throw new Error( - 'Subsequent execution after rejecting autocompletion setup prompted again' + - ' when it should not have.', - ); - } + assert.doesNotMatch( + stdout2, + AUTOCOMPLETION_PROMPT, + 'Subsequent execution after rejecting autocompletion setup prompted again' + + ' when it should not have.', + ); const bashrcContents = await fs.readFile(bashrc, 'utf-8'); - if (bashrcContents !== '# Other commands...') { - throw new Error( - '`~/.bashrc` file was incorrectly modified when the user never accepted the' + - ` autocompletion prompt. Contents:\n${bashrcContents}`, - ); - } + assert.strictEqual( + bashrcContents, + '# Other commands...', + '`~/.bashrc` file was incorrectly modified when the user never accepted the' + + ` autocompletion prompt. Contents:\n${bashrcContents}`, + ); }); // Prompts user again on subsequent execution after accepting prompt but failing to setup. @@ -220,11 +233,11 @@ export default async function () { 'y\n' /* stdin: accept prompt */, ); - if (!err.message.includes('Failed to append autocompletion setup')) { - throw new Error( - `Failed first execution did not print the expected error message. Actual:\n${err.message}`, - ); - } + assert.match( + err.message, + /Failed to append autocompletion setup/, + `Failed first execution did not print the expected error message. Actual:\n${err.message}`, + ); // User corrects file permissions between executions. await fs.chmod(bashrc, 0o777); @@ -240,20 +253,20 @@ export default async function () { 'y\n' /* stdin: accept prompt */, ); - if (!AUTOCOMPLETION_PROMPT.test(stdout2)) { - throw new Error( - 'Subsequent execution after failed autocompletion setup did not prompt again when it should' + - ' have.', - ); - } + assert.match( + stdout2, + AUTOCOMPLETION_PROMPT, + 'Subsequent execution after failed autocompletion setup did not prompt again when it should' + + ' have.', + ); const bashrcContents = await fs.readFile(bashrc, 'utf-8'); - if (!bashrcContents.includes('ng completion script')) { - throw new Error( - '`~/.bashrc` file does not include `ng completion` after the user never accepted the' + - ` autocompletion prompt a second time. Contents:\n${bashrcContents}`, - ); - } + assert.match( + bashrcContents, + /ng completion script/, + '`~/.bashrc` file does not include `ng completion` after the user never accepted the' + + ` autocompletion prompt a second time. Contents:\n${bashrcContents}`, + ); }); // Does *not* prompt for `ng update` commands. @@ -264,9 +277,11 @@ export default async function () { HOME: home, }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error('`ng update` command incorrectly prompted for autocompletion setup.'); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + '`ng update` command incorrectly prompted for autocompletion setup.', + ); }); // Does *not* prompt for `ng completion` commands. @@ -276,9 +291,11 @@ export default async function () { HOME: home, }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error('`ng completion` command incorrectly prompted for autocompletion setup.'); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + '`ng completion` command incorrectly prompted for autocompletion setup.', + ); }); // Does *not* prompt user for CI executions. @@ -289,9 +306,11 @@ export default async function () { NG_FORCE_TTY: undefined, }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error('CI execution prompted for autocompletion setup but should not have.'); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + 'CI execution prompted for autocompletion setup but should not have.', + ); } // Does *not* prompt user for non-TTY executions. @@ -301,9 +320,11 @@ export default async function () { NG_FORCE_TTY: 'false', }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error('Non-TTY execution prompted for autocompletion setup but should not have.'); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + 'Non-TTY execution prompted for autocompletion setup but should not have.', + ); } // Does *not* prompt user for executions without a `$HOME`. @@ -313,12 +334,12 @@ export default async function () { HOME: undefined, }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error( - 'Execution without a `$HOME` value prompted for autocompletion setup but' + - ' should not have.', - ); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + 'Execution without a `$HOME` value prompted for autocompletion setup but' + + ' should not have.', + ); } // Does *not* prompt user for executions without a `$SHELL`. @@ -328,12 +349,12 @@ export default async function () { SHELL: undefined, }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error( - 'Execution without a `$SHELL` value prompted for autocompletion setup but' + - ' should not have.', - ); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + 'Execution without a `$SHELL` value prompted for autocompletion setup but' + + ' should not have.', + ); } // Does *not* prompt user for executions from unknown shells. @@ -343,12 +364,12 @@ export default async function () { SHELL: '/usr/bin/unknown', }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error( - 'Execution with an unknown `$SHELL` value prompted for autocompletion setup' + - ' but should not have.', - ); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + 'Execution with an unknown `$SHELL` value prompted for autocompletion setup' + + ' but should not have.', + ); } // Does *not* prompt user when an RC file already uses `ng completion`. @@ -370,12 +391,12 @@ source <(ng completion script) HOME: home, }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error( - "Execution with an existing `ng completion` line in the user's RC file" + - ' prompted for autocompletion setup but should not have.', - ); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + "Execution with an existing `ng completion` line in the user's RC file" + + ' prompted for autocompletion setup but should not have.', + ); }); // Prompts when a global CLI install is present on the system. @@ -418,12 +439,12 @@ source <(ng completion script) PATH: pathEnvVar, }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error( - 'Execution without a global CLI install prompted for autocompletion setup but should' + - ' not have.', - ); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + 'Execution without a global CLI install prompted for autocompletion setup but should' + + ' not have.', + ); } finally { // Reinstall global CLI for remainder of the tests. await silentNpm(['install', '--global', '@angular/cli', `--registry=${testRegistry}`]); @@ -439,11 +460,11 @@ async function windowsTests(): Promise { const { stdout } = await execWithEnv('ng', ['config'], { ...env }); - if (AUTOCOMPLETION_PROMPT.test(stdout)) { - throw new Error( - 'Execution prompted to set up autocompletion on Windows despite not actually being' + - ' supported.', - ); - } + assert.doesNotMatch( + stdout, + AUTOCOMPLETION_PROMPT, + 'Execution prompted to set up autocompletion on Windows despite not actually being' + + ' supported.', + ); }); } diff --git a/tests/legacy-cli/e2e/tests/commands/completion/completion-script.ts b/tests/legacy-cli/e2e/tests/commands/completion/completion-script.ts index 1b940056d30e..421763478950 100644 --- a/tests/legacy-cli/e2e/tests/commands/completion/completion-script.ts +++ b/tests/legacy-cli/e2e/tests/commands/completion/completion-script.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { exec, execAndWaitForOutputToMatch } from '../../../utils/process'; export default async function () { @@ -62,9 +63,9 @@ export default async function () { 'run', 'test-project:build', ); - if (noServeStdout.includes(':serve')) { - throw new Error( - `':serve' should not have been listed as a completion option.\nSTDOUT:\n${noServeStdout}`, - ); - } + assert.doesNotMatch( + noServeStdout, + /:serve/, + `':serve' should not have been listed as a completion option.\nSTDOUT:\n${noServeStdout}`, + ); } diff --git a/tests/legacy-cli/e2e/tests/commands/config/config-get.ts b/tests/legacy-cli/e2e/tests/commands/config/config-get.ts index 51d5f5c7aa1e..d64e0a630af0 100644 --- a/tests/legacy-cli/e2e/tests/commands/config/config-get.ts +++ b/tests/legacy-cli/e2e/tests/commands/config/config-get.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { ng } from '../../../utils/process'; import { expectToFail } from '../../../utils/utils'; @@ -5,40 +6,29 @@ export default async function () { await expectToFail(() => ng('config', 'schematics.@schematics/angular.component.inlineStyle')); await ng('config', 'schematics.@schematics/angular.component.inlineStyle', 'false'); const { stdout } = await ng('config', 'schematics.@schematics/angular.component.inlineStyle'); - if (!stdout.match(/false\n?/)) { - throw new Error(`Expected "false", received "${JSON.stringify(stdout)}".`); - } + assert.match(stdout, /false\n?/); await ng('config', 'schematics.@schematics/angular.component.inlineStyle', 'true'); const { stdout: stdout1 } = await ng( 'config', 'schematics.@schematics/angular.component.inlineStyle', ); - if (!stdout1.match(/true\n?/)) { - throw new Error(`Expected "true", received "${JSON.stringify(stdout)}".`); - } + assert.match(stdout1, /true\n?/); await ng('config', 'schematics.@schematics/angular.component.inlineStyle', 'false'); const { stdout: stdout2 } = await ng( 'config', `projects.test-project.architect.build.options.assets[0]`, ); - if (!stdout2.includes('"input": "public"')) { - throw new Error(`Expected "input": "public", received "${JSON.stringify(stdout)}".`); - } + assert.ok(stdout2.includes('"input": "public"')); const { stdout: stdout3 } = await ng( 'config', `projects["test-project"].architect.build.options.assets[0]`, ); - - if (!stdout3.includes('"input": "public"')) { - throw new Error(`Expected "input": "public", received "${JSON.stringify(stdout)}".`); - } + assert.ok(stdout3.includes('"input": "public"')); // should print all config when no positional args are provided. const { stdout: stdout4 } = await ng('config'); - if (!stdout4.includes('$schema')) { - throw new Error(`Expected to contain "$schema", received "${JSON.stringify(stdout)}".`); - } + assert.ok(stdout4.includes('$schema')); } diff --git a/tests/legacy-cli/e2e/tests/commands/config/config-global-validation.ts b/tests/legacy-cli/e2e/tests/commands/config/config-global-validation.ts index 05ba35ab7e9a..7be29130dca0 100644 --- a/tests/legacy-cli/e2e/tests/commands/config/config-global-validation.ts +++ b/tests/legacy-cli/e2e/tests/commands/config/config-global-validation.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { homedir } from 'node:os'; import * as path from 'node:path'; import { deleteFile, expectFileToExist } from '../../../utils/fs'; @@ -8,43 +9,28 @@ export default async function () { let ngError: Error; ngError = await expectToFail(() => silentNg('config', 'cli.completion.prompted', 'true')); - - if ( - !ngError.message.includes('Data path "/cli" must NOT have additional properties(completion).') - ) { - throw new Error('Should have failed with must NOT have additional properties(completion).'); - } + assert.match( + ngError.message, + /Data path "\/cli" must NOT have additional properties\(completion\)\./, + ); ngError = await expectToFail(() => silentNg('config', '--global', 'cli.completion.invalid', 'true'), ); - - if ( - !ngError.message.includes( - 'Data path "/cli/completion" must NOT have additional properties(invalid).', - ) - ) { - throw new Error('Should have failed with must NOT have additional properties(invalid).'); - } + assert.match( + ngError.message, + /Data path "\/cli\/completion" must NOT have additional properties\(invalid\)\./, + ); ngError = await expectToFail(() => silentNg('config', '--global', 'cli.cache.enabled', 'true')); - - if (!ngError.message.includes('Data path "/cli" must NOT have additional properties(cache).')) { - throw new Error('Should have failed with must NOT have additional properties(cache).'); - } + assert.match(ngError.message, /Data path "\/cli" must NOT have additional properties\(cache\)\./); ngError = await expectToFail(() => silentNg('config', 'cli.completion.prompted')); - - if (!ngError.message.includes('Value cannot be found.')) { - throw new Error('Should have failed with Value cannot be found.'); - } + assert.match(ngError.message, /Value cannot be found\./); await ng('config', '--global', 'cli.completion.prompted', 'true'); const { stdout } = await silentNg('config', '--global', 'cli.completion.prompted'); - - if (!stdout.includes('true')) { - throw new Error(`Expected "true", received "${JSON.stringify(stdout)}".`); - } + assert.match(stdout, /true/); await expectFileToExist(path.join(homedir(), '.angular-config.json')); await deleteFile(path.join(homedir(), '.angular-config.json')); diff --git a/tests/legacy-cli/e2e/tests/commands/config/config-global.ts b/tests/legacy-cli/e2e/tests/commands/config/config-global.ts index 77f276b04146..030d4b583d2c 100644 --- a/tests/legacy-cli/e2e/tests/commands/config/config-global.ts +++ b/tests/legacy-cli/e2e/tests/commands/config/config-global.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { homedir } from 'node:os'; import * as path from 'node:path'; import { deleteFile, expectFileToExist } from '../../../utils/fs'; @@ -15,9 +16,7 @@ export default async function () { '--global', 'schematics.@schematics/angular.component.inlineStyle', ); - if (!output.stdout.match(/false\n?/)) { - throw new Error(`Expected "false", received "${JSON.stringify(output.stdout)}".`); - } + assert.match(output.stdout, /false\n?/); // This test requires schema querying capabilities // .then(() => expectToFail(() => { @@ -33,9 +32,7 @@ export default async function () { } output = await ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle'); - if (!output.stdout.match(/true\n?/)) { - throw new Error(`Expected "true", received "${JSON.stringify(output.stdout)}".`); - } + assert.match(output.stdout, /true\n?/); await expectToFail(() => ng('config', '--global', 'cli.warnings.notreal', 'true')); diff --git a/tests/legacy-cli/e2e/tests/commands/config/config-set-prefix.ts b/tests/legacy-cli/e2e/tests/commands/config/config-set-prefix.ts index 09c3afea9f7f..40f22699baed 100644 --- a/tests/legacy-cli/e2e/tests/commands/config/config-set-prefix.ts +++ b/tests/legacy-cli/e2e/tests/commands/config/config-set-prefix.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { ng } from '../../../utils/process'; import { expectToFail } from '../../../utils/utils'; @@ -7,8 +8,6 @@ export default function () { .then(() => ng('config', 'schematics.@schematics/angular.component.prefix', 'new-prefix')) .then(() => ng('config', 'schematics.@schematics/angular.component.prefix')) .then(({ stdout }) => { - if (!stdout.match(/new-prefix/)) { - throw new Error(`Expected "new-prefix", received "${JSON.stringify(stdout)}".`); - } + assert.match(stdout, /new-prefix/); }); } diff --git a/tests/legacy-cli/e2e/tests/commands/config/config-set.ts b/tests/legacy-cli/e2e/tests/commands/config/config-set.ts index 689be8c72194..2152e573132e 100644 --- a/tests/legacy-cli/e2e/tests/commands/config/config-set.ts +++ b/tests/legacy-cli/e2e/tests/commands/config/config-set.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { ng, silentNg } from '../../../utils/process'; import { expectToFail } from '../../../utils/utils'; @@ -5,36 +6,25 @@ export default async function () { let ngError: Error; ngError = await expectToFail(() => silentNg('config', 'cli.warnings.zzzz', 'true')); - if ( - !ngError.message.includes( - 'Data path "/cli/warnings" must NOT have additional properties(zzzz).', - ) - ) { - throw new Error('Should have failed with must NOT have additional properties(zzzz).'); - } + assert.match( + ngError.message, + /Data path "\/cli\/warnings" must NOT have additional properties\(zzzz\)\./, + ); ngError = await expectToFail(() => silentNg('config', 'cli.warnings.zzzz')); - if (!ngError.message.includes('Value cannot be found.')) { - throw new Error('Should have failed with Value cannot be found.'); - } + assert.match(ngError.message, /Value cannot be found\./); await ng('config', 'cli.warnings.versionMismatch', 'false'); const { stdout } = await ng('config', 'cli.warnings.versionMismatch'); - if (!stdout.includes('false')) { - throw new Error(`Expected "false", received "${JSON.stringify(stdout)}".`); - } + assert.match(stdout, /false/); await ng('config', 'cli.packageManager', 'yarn'); const { stdout: stdout2 } = await ng('config', 'cli.packageManager'); - if (!stdout2.includes('yarn')) { - throw new Error(`Expected "yarn", received "${JSON.stringify(stdout2)}".`); - } + assert.match(stdout2, /yarn/); await ng('config', 'schematics', '{"@schematics/angular:component":{"style": "scss"}}'); const { stdout: stdout3 } = await ng('config', 'schematics.@schematics/angular:component.style'); - if (!stdout3.includes('scss')) { - throw new Error(`Expected "scss", received "${JSON.stringify(stdout3)}".`); - } + assert.match(stdout3, /scss/); await ng('config', 'schematics'); await ng('config', 'schematics', 'undefined'); diff --git a/tests/legacy-cli/e2e/tests/commands/help/help-hidden.ts b/tests/legacy-cli/e2e/tests/commands/help/help-hidden.ts index bf616039600b..bd972d26a942 100644 --- a/tests/legacy-cli/e2e/tests/commands/help/help-hidden.ts +++ b/tests/legacy-cli/e2e/tests/commands/help/help-hidden.ts @@ -1,15 +1,14 @@ +import assert from 'node:assert/strict'; import { silentNg } from '../../../utils/process'; export default async function () { const { stdout: stdoutNew } = await silentNg('--help'); - if (/(easter-egg)|(ng make-this-awesome)|(ng init)/.test(stdoutNew)) { - throw new Error( - 'Expected to not match "(easter-egg)|(ng make-this-awesome)|(ng init)" in help output.', - ); - } + assert.doesNotMatch( + stdoutNew, + /(easter-egg)|(ng make-this-awesome)|(ng init)/, + 'Expected to not match "(easter-egg)|(ng make-this-awesome)|(ng init)" in help output.', + ); const { stdout: ngGenerate } = await silentNg('--help', 'generate', 'component'); - if (ngGenerate.includes('--path')) { - throw new Error('Expected to not match "--path" in help output.'); - } + assert.doesNotMatch(ngGenerate, /--path/, 'Expected to not match "--path" in help output.'); } diff --git a/tests/legacy-cli/e2e/tests/commands/help/help-json.ts b/tests/legacy-cli/e2e/tests/commands/help/help-json.ts index 97ed468df942..6f1b8e89db49 100644 --- a/tests/legacy-cli/e2e/tests/commands/help/help-json.ts +++ b/tests/legacy-cli/e2e/tests/commands/help/help-json.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { silentNg } from '../../../utils/process'; export default async function () { @@ -42,31 +43,21 @@ export default async function () { const { stdout } = await silentNg('config', '--help', '--json-help'); const output = JSON.stringify(JSON.parse(stdout.trim())); - if (output !== addHelpOutputSnapshot) { - throw new Error( - `ng config JSON help output didn\'t match snapshot.\n\nExpected "${output}" to be "${addHelpOutputSnapshot}".`, - ); - } + assert.strictEqual( + output, + addHelpOutputSnapshot, + `ng config JSON help output didn\'t match snapshot.`, + ); const { stdout: stdout2 } = await silentNg('--help', '--json-help'); - try { - JSON.parse(stdout2.trim()); - } catch (error) { - throw new Error( - `'ng --help ---json-help' failed to return JSON.\n${ - error instanceof Error ? error.message : error - }`, - ); - } + assert.doesNotThrow( + () => JSON.parse(stdout2.trim()), + `'ng --help ---json-help' failed to return JSON.`, + ); const { stdout: stdout3 } = await silentNg('generate', '--help', '--json-help'); - try { - JSON.parse(stdout3.trim()); - } catch (error) { - throw new Error( - `'ng generate --help ---json-help' failed to return JSON.\n${ - error instanceof Error ? error.message : error - }`, - ); - } + assert.doesNotThrow( + () => JSON.parse(stdout3.trim()), + `'ng generate --help ---json-help' failed to return JSON.`, + ); } diff --git a/tests/legacy-cli/e2e/tests/commands/project-cannot-be-determined-by-cwd.ts b/tests/legacy-cli/e2e/tests/commands/project-cannot-be-determined-by-cwd.ts index 5f63a5ff0467..b4b572bfa3ee 100644 --- a/tests/legacy-cli/e2e/tests/commands/project-cannot-be-determined-by-cwd.ts +++ b/tests/legacy-cli/e2e/tests/commands/project-cannot-be-determined-by-cwd.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { join } from 'node:path'; import { execAndWaitForOutputToMatch, ng } from '../../utils/process'; import { updateJsonFile } from '../../utils/project'; @@ -20,9 +21,7 @@ export default async function () { try { const { message } = await expectToFail(() => ng('build')); - if (!message.includes(errorMessage)) { - throw new Error(`Expected build to fail with: '${errorMessage}'.`); - } + assert.match(message, new RegExp(errorMessage)); // Help should still work await execAndWaitForOutputToMatch('ng', ['build', '--help'], /--configuration/); diff --git a/tests/legacy-cli/e2e/tests/commands/run-configuration-option.ts b/tests/legacy-cli/e2e/tests/commands/run-configuration-option.ts index 827d8dea6fc9..ab2761dce531 100644 --- a/tests/legacy-cli/e2e/tests/commands/run-configuration-option.ts +++ b/tests/legacy-cli/e2e/tests/commands/run-configuration-option.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { silentNg } from '../../utils/process'; import { expectToFail } from '../../utils/utils'; @@ -9,9 +10,7 @@ export default async function () { silentNg('run', 'test-project:build:development', '--configuration=production'), ); - if (!message.includes(errorMatch)) { - throw new Error(`Expected error to include '${errorMatch}' but didn't.\n\n${message}`); - } + assert.ok(message.includes(errorMatch)); } { @@ -19,8 +18,6 @@ export default async function () { silentNg('run', 'test-project:build', '--configuration=production'), ); - if (!message.includes(errorMatch)) { - throw new Error(`Expected error to include '${errorMatch}' but didn't.\n\n${message}`); - } + assert.ok(message.includes(errorMatch)); } } diff --git a/tests/legacy-cli/e2e/tests/commands/serve/head-request.ts b/tests/legacy-cli/e2e/tests/commands/serve/head-request.ts index 82ba370a0743..ed2c2dcd1267 100644 --- a/tests/legacy-cli/e2e/tests/commands/serve/head-request.ts +++ b/tests/legacy-cli/e2e/tests/commands/serve/head-request.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { ngServe } from '../../../utils/project'; export default async function () { @@ -16,10 +17,10 @@ async function checkHeadForUrl(url: string): Promise { const result = await fetch(url, { method: 'HEAD' }); const content = await result.blob(); - if (content.size !== 0) { - throw new Error(`Expected "size" to be "0" but got "${content.size}".`); - } - if (result.status !== 200) { - throw new Error(`Expected "status" to be "200" but got "${result.status}".`); - } + assert.strictEqual(content.size, 0, `Expected "size" to be "0" but got "${content.size}".`); + assert.strictEqual( + result.status, + 200, + `Expected "status" to be "200" but got "${result.status}".`, + ); } diff --git a/tests/legacy-cli/e2e/tests/commands/serve/preflight-request.ts b/tests/legacy-cli/e2e/tests/commands/serve/preflight-request.ts index d73bf1eb4003..02a763dce197 100644 --- a/tests/legacy-cli/e2e/tests/commands/serve/preflight-request.ts +++ b/tests/legacy-cli/e2e/tests/commands/serve/preflight-request.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { ngServe } from '../../../utils/project'; export default async function () { @@ -5,11 +6,10 @@ export default async function () { const result = await fetch(`http://localhost:${port}/main.js`, { method: 'OPTIONS' }); const content = await result.blob(); - if (content.size !== 0) { - throw new Error(`Expected "size" to be "0" but got "${content.size}".`); - } - - if (result.status !== 204) { - throw new Error(`Expected "status" to be "204" but got "${result.status}".`); - } + assert.strictEqual(content.size, 0, `Expected "size" to be "0" but got "${content.size}".`); + assert.strictEqual( + result.status, + 204, + `Expected "status" to be "204" but got "${result.status}".`, + ); } diff --git a/tests/legacy-cli/e2e/tests/generate/help-output-no-duplicates.ts b/tests/legacy-cli/e2e/tests/generate/help-output-no-duplicates.ts index 9a8cf8ca7fcc..0be6ea93ea48 100644 --- a/tests/legacy-cli/e2e/tests/generate/help-output-no-duplicates.ts +++ b/tests/legacy-cli/e2e/tests/generate/help-output-no-duplicates.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { ng } from '../../utils/process'; export default async function () { @@ -5,13 +6,10 @@ export default async function () { const { stdout } = await ng('generate', 'component', '--help'); const firstIndex = stdout.indexOf('--prefix'); - if (firstIndex < 0) { - console.log(stdout); - throw new Error('--prefix was not part of the help output.'); - } - - if (firstIndex !== stdout.lastIndexOf('--prefix')) { - console.log(stdout); - throw new Error('--prefix first and last index were different. Possible duplicate output!'); - } + assert.ok(firstIndex >= 0, '--prefix was not part of the help output.'); + assert.strictEqual( + firstIndex, + stdout.lastIndexOf('--prefix'), + '--prefix first and last index were different. Possible duplicate output!', + ); } diff --git a/tests/legacy-cli/e2e/tests/generate/help-output.ts b/tests/legacy-cli/e2e/tests/generate/help-output.ts index 9495bfa200a6..86fc3b988b38 100644 --- a/tests/legacy-cli/e2e/tests/generate/help-output.ts +++ b/tests/legacy-cli/e2e/tests/generate/help-output.ts @@ -1,6 +1,7 @@ +import assert from 'node:assert/strict'; import { join } from 'node:path'; -import { ng, ProcessOutput } from '../../utils/process'; -import { writeMultipleFiles, createDir } from '../../utils/fs'; +import { createDir, writeMultipleFiles } from '../../utils/fs'; +import { ng } from '../../utils/process'; import { updateJsonFile } from '../../utils/project'; export default function () { @@ -75,12 +76,8 @@ export default function () { ) .then(() => ng('generate', 'fake-schematics:fake', '--help')) .then(({ stdout }) => { - if (!/ng generate fake-schematics:fake \[b\]/.test(stdout)) { - throw new Error('Help signature is wrong (1).'); - } - if (!/opt-a[\s\S]*opt-b[\s\S]*opt-c/.test(stdout)) { - throw new Error('Help signature options are incorrect.'); - } + assert.match(stdout, /ng generate fake-schematics:fake \[b\]/); + assert.match(stdout, /opt-a[\s\S]*opt-b[\s\S]*opt-c/); }) // set up default collection. .then(() => @@ -92,12 +89,8 @@ export default function () { .then(() => ng('generate', 'fake', '--help')) // verify same output .then(({ stdout }) => { - if (!/ng generate fake \[b\]/.test(stdout)) { - throw new Error('Help signature is wrong (2).'); - } - if (!/opt-a[\s\S]*opt-b[\s\S]*opt-c/.test(stdout)) { - throw new Error('Help signature options are incorrect.'); - } + assert.match(stdout, /ng generate fake \[b\]/); + assert.match(stdout, /opt-a[\s\S]*opt-b[\s\S]*opt-c/); }) // should print all the available schematics in a collection @@ -123,9 +116,7 @@ export default function () { ) .then(() => ng('generate', '--help')) .then(({ stdout }) => { - if (!/fake[\s\S]*fake-two/.test(stdout)) { - throw new Error(`Help result is wrong, it didn't contain all the schematics.`); - } + assert.match(stdout, /fake[\s\S]*fake-two/); }) ); } diff --git a/tests/legacy-cli/e2e/tests/generate/schematic-defaults.ts b/tests/legacy-cli/e2e/tests/generate/schematic-defaults.ts index 7c9987d92321..a6fc32a8c52e 100644 --- a/tests/legacy-cli/e2e/tests/generate/schematic-defaults.ts +++ b/tests/legacy-cli/e2e/tests/generate/schematic-defaults.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert/strict'; import { ng } from '../../utils/process'; import { updateJsonFile } from '../../utils/project'; @@ -12,9 +13,7 @@ export default async function () { // Generate component in application to verify that it's minimal const { stdout } = await ng('generate', 'component', 'foo'); - if (!stdout.includes('foo.scss')) { - throw new Error('Expected "foo.scss" to exist.'); - } + assert.match(stdout, /foo\.scss/); // Generate another project with different settings await ng('generate', 'application', 'test-project-two', '--no-minimal'); @@ -35,7 +34,5 @@ export default async function () { '--project', 'test-project-two', ); - if (!stdout2.includes('foo.component.less')) { - throw new Error('Expected "foo.component.less" to exist.'); - } + assert.match(stdout2, /foo\.component\.less/); } diff --git a/tests/legacy-cli/e2e/tests/generate/schematics-collections.ts b/tests/legacy-cli/e2e/tests/generate/schematics-collections.ts index f67dde1072c4..6c009bf91e5d 100644 --- a/tests/legacy-cli/e2e/tests/generate/schematics-collections.ts +++ b/tests/legacy-cli/e2e/tests/generate/schematics-collections.ts @@ -1,6 +1,7 @@ +import assert from 'node:assert/strict'; import { join } from 'node:path'; +import { createDir, expectFileToExist, writeMultipleFiles } from '../../utils/fs'; import { ng } from '../../utils/process'; -import { writeMultipleFiles, createDir, expectFileToExist } from '../../utils/fs'; import { updateJsonFile } from '../../utils/project'; export default async function () { @@ -55,24 +56,15 @@ export default async function () { // should display schematics for all schematics const { stdout: stdout1 } = await ng('generate', '--help'); - if (!stdout1.includes('ng generate component')) { - throw new Error(`Didn't show schematics of '@schematics/angular'.`); - } - - if (!stdout1.includes('ng generate fake')) { - throw new Error(`Didn't show schematics of 'fake-schematics'.`); - } + assert.match(stdout1, /ng generate component/); + assert.match(stdout1, /ng generate fake/); // check registration order. Both schematics contain a component schematic verify that the first one wins. - if (!stdout1.includes(fakeComponentSchematicDesc)) { - throw new Error(`Didn't show fake component description.`); - } + assert.match(stdout1, new RegExp(fakeComponentSchematicDesc)); // Verify execution based on ordering const { stdout: stdout2 } = await ng('generate', 'component'); - if (!stdout2.includes('fake component schematic run')) { - throw new Error(`stdout didn't contain 'fake component schematic run'.`); - } + assert.match(stdout2, /fake component schematic run/); await updateJsonFile('angular.json', (json) => { json.cli ??= {}; @@ -80,12 +72,8 @@ export default async function () { }); const { stdout: stdout3 } = await ng('generate', '--help'); - if (!stdout3.includes('ng generate component [name]')) { - throw new Error(`Didn't show component description from @schematics/angular.`); - } - if (stdout3.includes(fakeComponentSchematicDesc)) { - throw new Error(`Shown fake component description, when it shouldn't.`); - } + assert.match(stdout3, /ng generate component \[name\]/); + assert.doesNotMatch(stdout3, new RegExp(fakeComponentSchematicDesc)); // Verify execution based on ordering const projectDir = join('src', 'app');