print stack trace on calls to process.exit (#6714)

* print stack trace on calls to process.exit

* link PR in changelog

* guard exit handler against missing global process

* moar guard

* remove unnecessary newline
This commit is contained in:
Simen Bekkhus 2018-08-08 04:38:47 +02:00 committed by Aaron Abramov
parent f66b74d1e7
commit 690450da4c
6 changed files with 74 additions and 0 deletions

View File

@ -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))

View File

@ -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)
"
`;

View File

@ -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();
});

View File

@ -0,0 +1,5 @@
process.exit(1);
test('something', () => {
expect(true).toBe(true);
});

View File

@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}

View File

@ -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 = {
@ -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();