Skip to content

Commit 8aeb67d

Browse files
committed
Added test for throwing expect.JestAssertionError
1 parent 97b7e3d commit 8aeb67d

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

packages/expect/src/__tests__/stacktrace.test.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@ jestExpect.extend({
1616
pass: true,
1717
};
1818
},
19+
toPreserveErrorCallStack(callback) {
20+
try {
21+
callback();
22+
} catch (error) {
23+
const assertionError = new jestExpect.JestAssertionError(error.message);
24+
assertionError.stack = error.stack;
25+
throw assertionError;
26+
}
27+
},
1928
});
2029

2130
it('stack trace points to correct location when using matchers', () => {
2231
try {
2332
jestExpect(true).toBe(false);
2433
} catch (error) {
25-
expect(error.stack).toContain('stacktrace.test.js:23');
34+
expect(error.stack).toContain('stacktrace.test.js:32');
2635
}
2736
});
2837

@@ -32,6 +41,22 @@ it('stack trace points to correct location when using nested matchers', () => {
3241
jestExpect(value).toBe(false);
3342
});
3443
} catch (error) {
35-
expect(error.stack).toContain('stacktrace.test.js:32');
44+
expect(error.stack).toContain('stacktrace.test.js:41');
45+
}
46+
});
47+
48+
it('stack trace points to correct location when throwing an instance of JestAssertionError', () => {
49+
try {
50+
jestExpect(() => {
51+
const foo = () => bar();
52+
const bar = () => baz();
53+
const baz = () => {
54+
throw new Error('Expected');
55+
};
56+
57+
foo();
58+
}).toPreserveErrorCallStack();
59+
} catch (error) {
60+
expect(error.stack).toContain('stacktrace.test.js:54');
3661
}
3762
});

0 commit comments

Comments
 (0)