Skip to content

test: use node:assert in basic and build E2E tests #30764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions tests/legacy-cli/e2e/tests/basic/rebuild.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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/);
}
}
11 changes: 3 additions & 8 deletions tests/legacy-cli/e2e/tests/basic/serve.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert/strict';
import { killAllProcesses } from '../../utils/process';
import { ngServe } from '../../utils/project';

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

async function verifyResponse(port: number): Promise<void> {
const indexResponse = await fetch(`http://localhost:${port}/`);

if (!/<app-root><\/app-root>/.test(await indexResponse.text())) {
throw new Error('Response does not match expected value.');
}
assert.match(await indexResponse.text(), /<app-root><\/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.');
}
6 changes: 2 additions & 4 deletions tests/legacy-cli/e2e/tests/basic/styles-array.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
}
13 changes: 4 additions & 9 deletions tests/legacy-cli/e2e/tests/build/bundle-budgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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');
}
19 changes: 9 additions & 10 deletions tests/legacy-cli/e2e/tests/build/prod-build.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 () {
Expand Down
19 changes: 4 additions & 15 deletions tests/legacy-cli/e2e/tests/build/progress-and-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand All @@ -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));
}
}
44 changes: 18 additions & 26 deletions tests/legacy-cli/e2e/tests/build/rebuild-deps-type-check.ts
Original file line number Diff line number Diff line change
@@ -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\./
Expand Down Expand Up @@ -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.
Expand All @@ -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(() =>
Expand All @@ -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'/,
);
})
);
}
21 changes: 8 additions & 13 deletions tests/legacy-cli/e2e/tests/build/relative-sourcemap.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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.');
}
12 changes: 7 additions & 5 deletions tests/legacy-cli/e2e/tests/build/scripts-output-hashing.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert/strict';
import { getGlobalVariable } from '../../utils/env';
import {
expectFileMatchToExist,
Expand Down Expand Up @@ -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!',
);
}
25 changes: 12 additions & 13 deletions tests/legacy-cli/e2e/tests/build/sourcemap.ts
Original file line number Diff line number Diff line change
@@ -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'];
Expand Down Expand Up @@ -30,9 +31,7 @@ async function testForSourceMaps(expectedNumberOfFiles: number): Promise<void> {

++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');
Expand All @@ -41,15 +40,15 @@ async function testForSourceMaps(expectedNumberOfFiles: number): Promise<void> {
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}`,
);
}
7 changes: 3 additions & 4 deletions tests/legacy-cli/e2e/tests/build/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -83,9 +84,7 @@ async function getWorkerOutputFile(useWebpackBuilder: boolean): Promise<string>
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;
}
Loading