Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print stack trace on calls to process.exit #6714

Merged
merged 5 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

- `[babel-jest]` Make `getCacheKey()` take into account `createTransformer` options ([#6699](https://github.com/facebook/jest/pull/6699))
Expand Down
15 changes: 15 additions & 0 deletions e2e/__tests__/__snapshots__/process_exit.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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"
}
}
23 changes: 23 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,28 @@ async function runTestInternal(
// For runtime errors
sourcemapSupport.install(sourcemapOptions);

const realExit = environment.global.process.exit;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there's no process in global? We'll be calling undefined fn, or am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's always there because of the installCommonGlobals call. Of course fully custom envs won't necessarily have it...

I can wrap it in an if


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,
);

global.process.stderr.write(formattedError + '\n');

return realExit(...args);
};

try {
await environment.setup();

Expand Down