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

Wrap Error.captureStackTrace in a try #4035

Merged
merged 6 commits into from
Jul 14, 2017
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

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

describe('Stack Trace', () => {
it('prints a usable stack trace even if no Error.captureStackTrace', () => {
const result = runJest('stack_trace_no_captureStackTrace');
const stderr = result.stderr.toString();

const assertErrorLines = stderr.split('\n').slice(3, 9);

expect(result.status).toBe(1);

expect(stderr).toMatch(/\s+at\sJestAssertionError\s.*/);

expect(assertErrorLines).toEqual([
' expect(received).toBe(expected)',
' ',
' Expected value to be (using ===):',
' 2',
' Received:',
' 1',
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

Error.captureStackTrace = undefined;

test('some test', () => {
expect(1).toBe(2);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
14 changes: 10 additions & 4 deletions packages/jest-matchers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,11 @@ const makeThrowingMatcher = (
try {
result = matcher.apply(matcherContext, [actual].concat(args));
} catch (error) {
// Remove this and deeper functions from the stack trace frame.
Error.captureStackTrace(error, throwingMatcher);
// Try to remove this and deeper functions from the stack trace frame.
// Guard for some environments (browsers) that do not support this feature.
if (Error.captureStackTrace) {
Error.captureStackTrace(error, throwingMatcher);
}
throw error;
}

Expand All @@ -211,8 +214,11 @@ const makeThrowingMatcher = (
// reporter could access the actual and expected objects of the result
// for example in order to display a custom visual diff
error.matcherResult = result;
// Remove this function from the stack trace frame.
Error.captureStackTrace(error, throwingMatcher);
// Try to remove this function from the stack trace frame.
// Guard for some environments (browsers) that do not support this feature.
if (Error.captureStackTrace) {
Error.captureStackTrace(error, throwingMatcher);
}

if (throws) {
throw error;
Expand Down