Skip to content

Commit

Permalink
print stack trace on calls to process.exit (#6714)
Browse files Browse the repository at this point in the history
* print stack trace on calls to process.exit

* link PR in changelog

* guard exit handler against missing global process

* moar guard

* remove unnecessary newline
  • Loading branch information
SimenB authored and aaronabramov committed Aug 8, 2018
1 parent f66b74d commit 690450d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master

### Features

- `[jest-runner]` print stack trace when `process.exit` is called from user code ([#6714](https://github.com/facebook/jest/pull/6714))

### Fixes

- `[jest-snapshot` Mark snapshots as obsolete when moved to an inline snapshot ([#6773](https://github.com/facebook/jest/pull/6773))
Expand Down
14 changes: 14 additions & 0 deletions e2e/__tests__/__snapshots__/process_exit.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints stack trace pointing to process.exit call 1`] = `
" ● process.exit called with \\"1\\"
> 1 | process.exit(1);
| ^
2 |
3 | test('something', () => {
4 | expect(true).toBe(true);
at Object.<anonymous> (__tests__/test.js:1:9)
"
`;
17 changes: 17 additions & 0 deletions e2e/__tests__/process_exit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
'use strict';

const runJest = require('../runJest');

it('prints stack trace pointing to process.exit call', async () => {
const {stderr} = await runJest('process-exit');

expect(stderr).toMatchSnapshot();
});
5 changes: 5 additions & 0 deletions e2e/process-exit/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
process.exit(1);

test('something', () => {
expect(true).toBe(true);
});
5 changes: 5 additions & 0 deletions e2e/process-exit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
29 changes: 29 additions & 0 deletions packages/jest-runner/src/run_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import LeakDetector from 'jest-leak-detector';
import {getTestEnvironment} from 'jest-config';
import * as docblock from 'jest-docblock';
import {formatExecError} from 'jest-message-util';
import sourcemapSupport from 'source-map-support';

type RunTestInternalResult = {
Expand Down Expand Up @@ -145,6 +146,34 @@ async function runTestInternal(
// For runtime errors
sourcemapSupport.install(sourcemapOptions);

if (
environment.global &&
environment.global.process &&
environment.global.process.exit
) {
const realExit = environment.global.process.exit;

environment.global.process.exit = function exit(...args) {
const error = new Error(`process.exit called with "${args.join(', ')}"`);

if (Error.captureStackTrace) {
Error.captureStackTrace(error, exit);
}

const formattedError = formatExecError(
error,
config,
{noStackTrace: false},
undefined,
true,
);

process.stderr.write(formattedError);

return realExit(...args);
};
}

try {
await environment.setup();

Expand Down

0 comments on commit 690450d

Please sign in to comment.