Skip to content

Commit

Permalink
chore(e2e): attempt to log better errors when running commands (#13881)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 9, 2023
1 parent 6d2632a commit 94b73a2
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions e2e/Utils.ts
Expand Up @@ -6,36 +6,31 @@
*/

import * as path from 'path';
import * as util from 'util';
import dedent = require('dedent');
import {ExecaReturnValue, sync as spawnSync} from 'execa';
import {ExecaSyncError, ExecaSyncReturnValue, sync as spawnSync} from 'execa';
import * as fs from 'graceful-fs';
import which = require('which');
import type {Config} from '@jest/types';

interface RunResult extends ExecaReturnValue {
status: number;
error: Error;
}
export const run = (
cmd: string,
cwd?: string,
env?: Record<string, string>,
): RunResult => {
): ExecaSyncReturnValue => {
const args = cmd.split(/\s/).slice(1);
const spawnOptions = {cwd, env, preferLocal: false, reject: false};
const result = spawnSync(cmd.split(/\s/)[0], args, spawnOptions) as RunResult;
const result = spawnSync(cmd.split(/\s/)[0], args, spawnOptions);

// For compat with cross-spawn
result.status = result.exitCode;
if (result.exitCode !== 0) {
const errorResult = result as ExecaSyncError;

if (result.status !== 0) {
throw new Error(dedent`
ORIGINAL CMD: ${cmd}
STDOUT: ${result.stdout}
STDERR: ${result.stderr}
STATUS: ${result.status}
ERROR: ${result.error}
`);
// have to extract message for the `util.inspect` to be useful for some reason
const {message, ...rest} = errorResult;

errorResult.message += `\n\n${util.inspect(rest)}`;

throw errorResult;
}

return result;
Expand Down

0 comments on commit 94b73a2

Please sign in to comment.