Skip to content

Commit 323b9a2

Browse files
authored
chore: add better error handling for failing e2e tests (#289)
1 parent 4320d3e commit 323b9a2

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

e2e/__tests__/uninstall.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test('uninstall fails when package is not defined', () => {
2424
"dependencies": {}
2525
}`,
2626
});
27-
const {stderr, code} = run(DIR, ['uninstall']);
27+
const {stderr, code} = run(DIR, ['uninstall'], {expectedFailure: true});
2828

2929
expect(stderr).toContain('missing required argument');
3030
expect(code).toBe(1);
@@ -36,7 +36,7 @@ test('uninstall fails when package is not installed', () => {
3636
"dependencies": {}
3737
}`,
3838
});
39-
const {stderr, code} = run(DIR, ['uninstall', pkg]);
39+
const {stderr, code} = run(DIR, ['uninstall', pkg], {expectedFailure: true});
4040

4141
expect(stderr).toContain(`Failed to unlink "${pkg}".`);
4242
expect(code).toBe(1);

e2e/helpers.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'path';
55
import {createDirectory} from 'jest-util';
66
import rimraf from 'rimraf';
77
import execa from 'execa';
8+
import chalk from 'chalk';
89
import {Writable} from 'readable-stream';
910

1011
const CLI_PATH = path.resolve(__dirname, '../packages/cli/build/bin.js');
@@ -13,12 +14,15 @@ type RunOptions = {
1314
nodeOptions?: string,
1415
nodePath?: string,
1516
timeout?: number, // kill the process after X milliseconds
17+
expectedFailure?: boolean,
1618
};
1719

1820
export function run(
1921
dir: string,
2022
args?: Array<string>,
21-
options: RunOptions = {},
23+
options: RunOptions = {
24+
expectedFailure: false,
25+
},
2226
) {
2327
return spawnCli(dir, args, options);
2428
}
@@ -28,7 +32,9 @@ export async function runUntil(
2832
dir: string,
2933
args: Array<string> | void,
3034
text: string,
31-
options: RunOptions = {},
35+
options: RunOptions = {
36+
expectedFailure: false,
37+
},
3238
) {
3339
const spawnPromise = spawnCliAsync(dir, args, {timeout: 30000, ...options});
3440

@@ -109,7 +115,11 @@ export const getTempDirectory = (name: string) =>
109115
function spawnCli(dir: string, args?: Array<string>, options: RunOptions = {}) {
110116
const {spawnArgs, spawnOptions} = getCliArguments({dir, args, options});
111117

112-
return execa.sync(process.execPath, spawnArgs, spawnOptions);
118+
const result = execa.sync(process.execPath, spawnArgs, spawnOptions);
119+
120+
handleTestCliFailure(options, result, dir, args);
121+
122+
return result;
113123
}
114124

115125
function spawnCliAsync(
@@ -119,7 +129,12 @@ function spawnCliAsync(
119129
) {
120130
const {spawnArgs, spawnOptions} = getCliArguments({dir, args, options});
121131

122-
return execa(process.execPath, spawnArgs, spawnOptions);
132+
try {
133+
return execa(process.execPath, spawnArgs, spawnOptions);
134+
} catch (result) {
135+
handleTestCliFailure(options, result, dir, args);
136+
return result;
137+
}
123138
}
124139

125140
function getCliArguments({dir, args, options}) {
@@ -147,3 +162,15 @@ function getCliArguments({dir, args, options}) {
147162
};
148163
return {spawnArgs, spawnOptions};
149164
}
165+
166+
function handleTestCliFailure(options, result, dir, args) {
167+
if (!options.expectedFailure && result.code !== 0) {
168+
console.log(`Running CLI failed for unexpected reason. Here's more info:
169+
170+
${chalk.bold('dir:')} ${dir}
171+
${chalk.bold('args:')} ${(args || []).join(' ')}
172+
${chalk.bold('stderr:')} ${result.stderr}
173+
${chalk.bold('stdout:')} ${result.stdout}
174+
${chalk.bold('code:')} ${result.code}`);
175+
}
176+
}

0 commit comments

Comments
 (0)