Skip to content

Commit fbacd19

Browse files
denniskecpojer
authored andcommitted
Do not modify stack trace of JestAssertionError (#4516)
* Do not modify stack trace of JestAssertionError. * Added test for stack trace of nested matchers. * Fixed test for nested matcher and added test for normal matchers. * Only comparing line numbers in test because function names in stack trace seem to be platform dependent.
1 parent 246da7f commit fbacd19

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
*/
9+
10+
const jestExpect = require('../');
11+
12+
jestExpect.extend({
13+
toMatchPredicate(received, argument) {
14+
argument(received);
15+
return {
16+
message: () => '',
17+
pass: true,
18+
};
19+
},
20+
});
21+
22+
it('stack trace points to correct location when using matchers', () => {
23+
try {
24+
jestExpect(true).toBe(false);
25+
} catch (error) {
26+
expect(error.stack).toContain('stacktrace.test.js:24');
27+
}
28+
});
29+
30+
it('stack trace points to correct location when using nested matchers', () => {
31+
try {
32+
jestExpect(true).toMatchPredicate(value => {
33+
jestExpect(value).toBe(false);
34+
});
35+
} catch (error) {
36+
expect(error.stack).toContain('stacktrace.test.js:33');
37+
}
38+
});

packages/expect/src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,12 @@ const makeThrowingMatcher = (
198198
try {
199199
result = matcher.apply(matcherContext, [actual].concat(args));
200200
} catch (error) {
201-
// Try to remove this and deeper functions from the stack trace frame.
202-
// Guard for some environments (browsers) that do not support this feature.
203-
if (Error.captureStackTrace) {
204-
Error.captureStackTrace(error, throwingMatcher);
201+
if (!(error instanceof JestAssertionError)) {
202+
// Try to remove this and deeper functions from the stack trace frame.
203+
// Guard for some environments (browsers) that do not support this feature.
204+
if (Error.captureStackTrace) {
205+
Error.captureStackTrace(error, throwingMatcher);
206+
}
205207
}
206208
throw error;
207209
}

0 commit comments

Comments
 (0)