diff --git a/tests/legacy-cli/e2e/tests/build/bundle-budgets.ts b/tests/legacy-cli/e2e/tests/build/bundle-budgets.ts index 2a6909413462..fec3dea7d93e 100644 --- a/tests/legacy-cli/e2e/tests/build/bundle-budgets.ts +++ b/tests/legacy-cli/e2e/tests/build/bundle-budgets.ts @@ -17,7 +17,7 @@ export default async function () { ]; }); - const errorMessage = await expectToFail(() => ng('build')); + const { message: errorMessage } = await expectToFail(() => ng('build')); if (!/Error.+budget/.test(errorMessage)) { throw new Error('Budget error: all, max error.'); } diff --git a/tests/legacy-cli/e2e/utils/process.ts b/tests/legacy-cli/e2e/utils/process.ts index f4f94bc09760..cb635fec5b55 100644 --- a/tests/legacy-cli/e2e/utils/process.ts +++ b/tests/legacy-cli/e2e/utils/process.ts @@ -29,8 +29,6 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise { - stdout += data.toString('utf-8'); - if (options.silent) { - return; - } - data - .toString('utf-8') - .split(/[\n\r]+/) - .filter((line) => line !== '') - .forEach((line) => console.log(' ' + line)); - }); - - childProcess.stderr!.on('data', (data: Buffer) => { - stderr += data.toString('utf-8'); - if (options.silent) { - return; - } - data - .toString('utf-8') - .split(/[\n\r]+/) - .filter((line) => line !== '') - .forEach((line) => console.error(colors.yellow(' ' + line))); - }); _processes.push(childProcess); // Create the error here so the stack shows who called this function. const error = new Error(); - // Return log info about the current process status - function envDump() { - return [ - `ENV:${JSON.stringify(spawnOptions.env, null, 2)}`, - `STDOUT:\n${stdout}`, - `STDERR:\n${stderr}`, - ].join('\n\n'); - } - return new Promise((resolve, reject) => { + let stdout = ''; + let stderr = ''; let matched = false; - childProcess.on('exit', (code: number) => { + // Return log info about the current process status + function envDump() { + return [ + `ENV:${JSON.stringify(spawnOptions.env, null, 2)}`, + `STDOUT:\n${stdout}`, + `STDERR:\n${stderr}`, + ].join('\n\n'); + } + + childProcess.stdout!.on('data', (data: Buffer) => { + stdout += data.toString('utf-8'); + + if (options.waitForMatch && stdout.match(options.waitForMatch)) { + resolve({ stdout, stderr }); + matched = true; + } + + if (options.silent) { + return; + } + + data + .toString('utf-8') + .split(/[\n\r]+/) + .filter((line) => line !== '') + .forEach((line) => console.log(' ' + line)); + }); + + childProcess.stderr!.on('data', (data: Buffer) => { + stderr += data.toString('utf-8'); + + if (options.waitForMatch && stderr.match(options.waitForMatch)) { + resolve({ stdout, stderr }); + matched = true; + } + + if (options.silent) { + return; + } + + data + .toString('utf-8') + .split(/[\n\r]+/) + .filter((line) => line !== '') + .forEach((line) => console.error(colors.yellow(' ' + line))); + }); + + childProcess.on('close', (code: number) => { _processes = _processes.filter((p) => p !== childProcess); if (options.waitForMatch && !matched) { @@ -134,24 +149,6 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise { - if (data.toString().match(match)) { - resolve({ stdout, stderr }); - matched = true; - } - }); - - childProcess.stderr!.on('data', (data: Buffer) => { - if (data.toString().match(match)) { - resolve({ stdout, stderr }); - matched = true; - } - }); - } - // Provide input to stdin if given. if (options.stdin) { childProcess.stdin!.write(options.stdin); @@ -192,14 +189,14 @@ export function waitForAnyProcessOutputToMatch( childProcess.stdout!.on('data', (data: Buffer) => { stdout += data.toString(); - if (data.toString().match(match)) { + if (stdout.match(match)) { resolve({ stdout, stderr }); } }); childProcess.stderr!.on('data', (data: Buffer) => { stderr += data.toString(); - if (data.toString().match(match)) { + if (stderr.match(match)) { resolve({ stdout, stderr }); } }); diff --git a/tests/legacy-cli/e2e/utils/utils.ts b/tests/legacy-cli/e2e/utils/utils.ts index b6e8374c464c..347e930a039e 100644 --- a/tests/legacy-cli/e2e/utils/utils.ts +++ b/tests/legacy-cli/e2e/utils/utils.ts @@ -2,7 +2,7 @@ import { mkdtemp, realpath, rm } from 'fs/promises'; import { tmpdir } from 'os'; import path from 'path'; -export function expectToFail(fn: () => Promise, errorMessage?: string): Promise { +export function expectToFail(fn: () => Promise, errorMessage?: string): Promise { return fn().then( () => { const functionSource = fn.name || (fn).source || fn.toString(); @@ -12,7 +12,7 @@ export function expectToFail(fn: () => Promise, errorMessage?: string): Pro ); }, (err) => { - return err; + return err instanceof Error ? err : new Error(err); }, ); }