Skip to content
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
4 changes: 2 additions & 2 deletions e2e/__tests__/uninstall.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('uninstall fails when package is not defined', () => {
"dependencies": {}
}`,
});
const {stderr, code} = run(DIR, ['uninstall']);
const {stderr, code} = run(DIR, ['uninstall'], {expectedFailure: true});

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

expect(stderr).toContain(`Project "${pkg}" is not a react-native library`);
expect(code).toBe(1);
Expand Down
35 changes: 31 additions & 4 deletions e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path';
import {createDirectory} from 'jest-util';
import rimraf from 'rimraf';
import execa from 'execa';
import chalk from 'chalk';
import {Writable} from 'readable-stream';

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

export function run(
dir: string,
args?: Array<string>,
options: RunOptions = {},
options: RunOptions = {
expectedFailure: false,
},
) {
return spawnCli(dir, args, options);
}
Expand All @@ -28,7 +32,9 @@ export async function runUntil(
dir: string,
args: Array<string> | void,
text: string,
options: RunOptions = {},
options: RunOptions = {
expectedFailure: false,
},
) {
const spawnPromise = spawnCliAsync(dir, args, {timeout: 30000, ...options});

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

return execa.sync(process.execPath, spawnArgs, spawnOptions);
const result = execa.sync(process.execPath, spawnArgs, spawnOptions);

handleTestCliFailure(options, result, dir, args);

return result;
}

function spawnCliAsync(
Expand All @@ -119,7 +129,12 @@ function spawnCliAsync(
) {
const {spawnArgs, spawnOptions} = getCliArguments({dir, args, options});

return execa(process.execPath, spawnArgs, spawnOptions);
try {
return execa(process.execPath, spawnArgs, spawnOptions);
} catch (result) {
handleTestCliFailure(options, result, dir, args);
return result;
}
}

function getCliArguments({dir, args, options}) {
Expand Down Expand Up @@ -147,3 +162,15 @@ function getCliArguments({dir, args, options}) {
};
return {spawnArgs, spawnOptions};
}

function handleTestCliFailure(options, result, dir, args) {
if (!options.expectedFailure && result.code !== 0) {
console.log(`Running CLI failed for unexpected reason. Here's more info:

${chalk.bold('dir:')} ${dir}
${chalk.bold('args:')} ${(args || []).join(' ')}
${chalk.bold('stderr:')} ${result.stderr}
${chalk.bold('stdout:')} ${result.stdout}
${chalk.bold('code:')} ${result.code}`);
}
}