Skip to content

Commit 03cce3d

Browse files
mjesuncpojer
authored andcommitted
Move from "process.exit" to "exit (#5313)
1 parent 158cd63 commit 03cce3d

File tree

13 files changed

+46
-27
lines changed

13 files changed

+46
-27
lines changed

packages/jest-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dependencies": {
77
"ansi-escapes": "^3.0.0",
88
"chalk": "^2.0.1",
9+
"exit": "^0.1.2",
910
"glob": "^7.1.2",
1011
"graceful-fs": "^4.1.11",
1112
"import-local": "^1.0.0",

packages/jest-cli/src/cli/index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {version as VERSION} from '../../package.json';
2222
import * as args from './args';
2323
import chalk from 'chalk';
2424
import createContext from '../lib/create_context';
25+
import exit from 'exit';
2526
import getChangedFilesPromise from '../get_changed_files_promise';
2627
import handleDeprecationWarnings from '../lib/handle_deprecation_warnings';
2728
import logDebugMessages from '../lib/log_debug_messages';
@@ -45,7 +46,7 @@ export async function run(maybeArgv?: Argv, project?: Path) {
4546
clearLine(process.stderr);
4647
clearLine(process.stdout);
4748
console.error(chalk.red(error.stack));
48-
process.exit(1);
49+
exit(1);
4950
throw error;
5051
}
5152
}
@@ -79,7 +80,7 @@ export const runCLI = async (
7980
process.stdout.write(`Cleared ${config.cacheDirectory}\n`);
8081
});
8182

82-
process.exit(0);
83+
exit(0);
8384
}
8485

8586
await _run(
@@ -111,9 +112,11 @@ const readResultsAndExit = (
111112
globalConfig: GlobalConfig,
112113
) => {
113114
const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
114-
process.on('exit', () => process.exit(code));
115+
116+
process.on('exit', () => exit(code));
117+
115118
if (globalConfig.forceExit) {
116-
process.exit(code);
119+
exit(code);
117120
}
118121
};
119122

@@ -164,13 +167,13 @@ const printDebugInfoAndExitIfNeeded = (
164167
logDebugMessages(globalConfig, configs, outputStream);
165168
}
166169
if (argv.showConfig) {
167-
process.exit(0);
170+
exit(0);
168171
}
169172
};
170173

171174
const printVersionAndExit = outputStream => {
172175
outputStream.write(`v${VERSION}\n`);
173-
process.exit(0);
176+
exit(0);
174177
};
175178

176179
const ensureNoDuplicateConfigs = (parsedConfigs, projects) => {
@@ -346,7 +349,7 @@ const runWatch = async (
346349
await handleDeprecationWarnings(outputStream, process.stdin);
347350
return watch(globalConfig, contexts, outputStream, hasteMapInstances);
348351
} catch (e) {
349-
process.exit(0);
352+
exit(0);
350353
}
351354
}
352355

packages/jest-cli/src/reporters/coverage_worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import type {GlobalConfig, ProjectConfig, Path} from 'types/Config';
1111

12+
import exit from 'exit';
1213
import fs from 'fs';
1314
import generateEmptyCoverage from '../generate_empty_coverage';
1415
import type {CoverageWorkerResult} from '../generate_empty_coverage';
@@ -24,7 +25,7 @@ export type {CoverageWorkerResult};
2425
// Make sure uncaught errors are logged before we exit.
2526
process.on('uncaughtException', err => {
2627
console.error(err.stack);
27-
process.exit(1);
28+
exit(1);
2829
});
2930

3031
export function worker({

packages/jest-cli/src/reporters/notify_reporter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {GlobalConfig} from 'types/Config';
1111
import type {AggregatedResult} from 'types/TestResult';
1212
import type {Context} from 'types/Context';
1313

14+
import exit from 'exit';
1415
import path from 'path';
1516
import util from 'util';
1617
import notifier from 'node-notifier';
@@ -73,7 +74,7 @@ export default class NotifyReporter extends BaseReporter {
7374
return;
7475
}
7576
if (metadata.activationValue === quitAnswer) {
76-
process.exit(0);
77+
exit(0);
7778
return;
7879
}
7980
if (metadata.activationValue === restartAnswer) {

packages/jest-cli/src/run_jest.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type TestWatcher from './test_watcher';
1616
import chalk from 'chalk';
1717
import path from 'path';
1818
import {Console, formatTestResults} from 'jest-util';
19+
import exit from 'exit';
1920
import fs from 'graceful-fs';
2021
import getNoTestsFoundMessage from './get_no_test_found_message';
2122
import SearchSource from './search_source';
@@ -111,7 +112,7 @@ export default (async function runJest({
111112
' is not supported without git/hg, please use --watchAll ' +
112113
'\n',
113114
);
114-
process.exit(1);
115+
exit(1);
115116
}
116117
}
117118

@@ -163,7 +164,7 @@ export default (async function runJest({
163164
} else {
164165
new Console(outputStream, outputStream).error(noTestsFoundMessage);
165166

166-
process.exit(1);
167+
exit(1);
167168
}
168169
} else if (
169170
allTests.length === 1 &&

packages/jest-cli/src/test_scheduler.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from './test_result_helpers';
2222
import CoverageReporter from './reporters/coverage_reporter';
2323
import DefaultReporter from './reporters/default_reporter';
24+
import exit from 'exit';
2425
import NotifyReporter from './reporters/notify_reporter';
2526
import ReporterDispatcher from './reporter_dispatcher';
2627
import snapshot from 'jest-snapshot';
@@ -324,11 +325,12 @@ export default class TestScheduler {
324325
if (watcher.isWatchMode()) {
325326
watcher.setState({interrupted: true});
326327
} else {
327-
const exit = () => process.exit(1);
328+
const failureExit = () => exit(1);
329+
328330
return this._dispatcher
329331
.onRunComplete(contexts, aggregatedResults)
330-
.then(exit)
331-
.catch(exit);
332+
.then(failureExit)
333+
.catch(failureExit);
332334
}
333335
}
334336
return Promise.resolve();

packages/jest-cli/src/watch.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {WatchPlugin} from './types';
1414
import ansiEscapes from 'ansi-escapes';
1515
import chalk from 'chalk';
1616
import getChangedFilesPromise from './get_changed_files_promise';
17+
import exit from 'exit';
1718
import {replacePathSepForRegex} from 'jest-regex-util';
1819
import HasteMap from 'jest-haste-map';
1920
import isValidPath from './lib/is_valid_path';
@@ -173,7 +174,7 @@ export default function watch(
173174
const onKeypress = (key: string) => {
174175
if (key === KEYS.CONTROL_C || key === KEYS.CONTROL_D) {
175176
outputStream.write('\n');
176-
process.exit(0);
177+
exit(0);
177178
return;
178179
}
179180

@@ -222,7 +223,7 @@ export default function watch(
222223
switch (key) {
223224
case KEYS.Q:
224225
outputStream.write('\n');
225-
process.exit(0);
226+
exit(0);
226227
return;
227228
case KEYS.ENTER:
228229
startRun(globalConfig);

packages/jest-runner/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"license": "MIT",
99
"main": "build/index.js",
1010
"dependencies": {
11+
"exit": "^0.1.2",
1112
"jest-config": "^22.1.0",
1213
"jest-docblock": "^22.1.0",
1314
"jest-haste-map": "^22.1.0",

packages/jest-runner/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919

2020
import typeof {worker} from './test_worker';
2121

22+
import exit from 'exit';
2223
import runTest from './run_test';
2324
import throat from 'throat';
2425
import Worker from 'jest-worker';
@@ -129,7 +130,7 @@ class TestRunner {
129130
'A worker process has quit unexpectedly! ' +
130131
'Most likely this is an initialization error.',
131132
);
132-
process.exit(1);
133+
exit(1);
133134
}
134135
};
135136

packages/jest-runner/src/test_worker.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';
1111
import type {SerializableError, TestResult} from 'types/TestResult';
1212
import type {RawModuleMap} from 'types/HasteMap';
1313

14-
// Make sure uncaught errors are logged before we exit.
15-
process.on('uncaughtException', err => {
16-
console.error(err.stack);
17-
process.exit(1);
18-
});
19-
14+
import exit from 'exit';
2015
import HasteMap from 'jest-haste-map';
2116
import {separateMessageFromStack} from 'jest-message-util';
2217
import Runtime from 'jest-runtime';
@@ -29,6 +24,12 @@ export type WorkerData = {|
2924
rawModuleMap: ?RawModuleMap,
3025
|};
3126

27+
// Make sure uncaught errors are logged before we exit.
28+
process.on('uncaughtException', err => {
29+
console.error(err.stack);
30+
exit(1);
31+
});
32+
3233
const formatError = (error: string | Error): SerializableError => {
3334
if (typeof error === 'string') {
3435
const {message, stack} = separateMessageFromStack(error);

packages/jest-runtime/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"babel-plugin-istanbul": "^4.1.5",
1414
"chalk": "^2.0.1",
1515
"convert-source-map": "^1.4.0",
16+
"exit": "^0.1.2",
1617
"graceful-fs": "^4.1.11",
1718
"jest-config": "^22.1.0",
1819
"jest-haste-map": "^22.1.0",

packages/jest-runtime/src/cli/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
import type {Argv} from 'types/Argv';
1111
import type {EnvironmentClass} from 'types/Environment';
1212

13+
import chalk from 'chalk';
14+
import exit from 'exit';
1315
import os from 'os';
1416
import path from 'path';
15-
import chalk from 'chalk';
1617
import yargs from 'yargs';
1718
import {Console, setGlobal, validateCLIOptions} from 'jest-util';
1819
import {readConfig} from 'jest-config';
@@ -42,7 +43,7 @@ export function run(cliArgv?: Argv, cliInfo?: Array<string>) {
4243

4344
if (argv.help) {
4445
yargs.showHelp();
45-
process.on('exit', () => process.exit(1));
46+
process.on('exit', () => exit(1));
4647
return;
4748
}
4849

@@ -53,7 +54,7 @@ export function run(cliArgv?: Argv, cliInfo?: Array<string>) {
5354

5455
if (!argv._.length) {
5556
console.log('Please provide a path to a script. (See --help for details)');
56-
process.on('exit', () => process.exit(1));
57+
process.on('exit', () => exit(1));
5758
return;
5859
}
5960

@@ -92,6 +93,6 @@ export function run(cliArgv?: Argv, cliInfo?: Array<string>) {
9293
})
9394
.catch(e => {
9495
console.error(chalk.red(e.stack || e));
95-
process.on('exit', () => process.exit(1));
96+
process.on('exit', () => exit(1));
9697
});
9798
}

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,6 +3065,10 @@ execa@^0.8.0:
30653065
signal-exit "^3.0.0"
30663066
strip-eof "^1.0.0"
30673067

3068+
exit@^0.1.2:
3069+
version "0.1.2"
3070+
resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c"
3071+
30683072
expand-braces@^0.1.1:
30693073
version "0.1.2"
30703074
resolved "https://registry.yarnpkg.com/expand-braces/-/expand-braces-0.1.2.tgz#488b1d1d2451cb3d3a6b192cfc030f44c5855fea"

0 commit comments

Comments
 (0)