Skip to content

Commit

Permalink
Wrap Error.captureStackTrace in a try (jestjs#4035)
Browse files Browse the repository at this point in the history
* Wrap `Error.captureStackTrace` in a try

Ref jestjs#4027

* Remove console.log

* Add comment

* Improve test

* Readd assertion

* Last assertion tweak
  • Loading branch information
SimenB authored and cpojer committed Jul 14, 2017
1 parent ae6eeda commit 867592b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
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

0 comments on commit 867592b

Please sign in to comment.