diff --git a/integration_tests/__tests__/stack_trace_no_captureStackTrace.test.js b/integration_tests/__tests__/stack_trace_no_captureStackTrace.test.js new file mode 100644 index 000000000000..f57fa7d270cb --- /dev/null +++ b/integration_tests/__tests__/stack_trace_no_captureStackTrace.test.js @@ -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', + ]); + }); +}); diff --git a/integration_tests/stack_trace_no_captureStackTrace/__tests__/runtime_error.test.js b/integration_tests/stack_trace_no_captureStackTrace/__tests__/runtime_error.test.js new file mode 100644 index 000000000000..05ef466d94ba --- /dev/null +++ b/integration_tests/stack_trace_no_captureStackTrace/__tests__/runtime_error.test.js @@ -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); +}); diff --git a/integration_tests/stack_trace_no_captureStackTrace/package.json b/integration_tests/stack_trace_no_captureStackTrace/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/integration_tests/stack_trace_no_captureStackTrace/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-matchers/src/index.js b/packages/jest-matchers/src/index.js index 4344a0f568f1..bc1397202542 100644 --- a/packages/jest-matchers/src/index.js +++ b/packages/jest-matchers/src/index.js @@ -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; } @@ -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;