Skip to content

Commit 338be2a

Browse files
committed
feat(jest-snapshot): Support Error.cause in snapshots
1 parent bb39cb2 commit 338be2a

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

e2e/__tests__/__snapshots__/toThrowErrorMatchingInlineSnapshot.test.ts.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@ exports[`updates existing snapshot: updated snapshot 1`] = `
1818
"
1919
`;
2020
21+
exports[`works fine when function throws error with cause: initial write with cause 1`] = `
22+
"test('works fine when function throws error', () => {
23+
expect(() => {
24+
throw new Error('apple', {
25+
cause: new Error('banana', {
26+
cause: new Error('orange'),
27+
}),
28+
});
29+
}).toThrowErrorMatchingInlineSnapshot(\`
30+
"apple
31+
Cause: banana
32+
Cause: orange"
33+
\`);
34+
});
35+
"
36+
`;
37+
38+
exports[`works fine when function throws error with string cause: initial write with cause 1`] = `
39+
"test('works fine when function throws error', () => {
40+
expect(() => {
41+
throw new Error('apple', {
42+
cause: 'here is a cause',
43+
});
44+
}).toThrowErrorMatchingInlineSnapshot(\`
45+
"apple
46+
Cause: here is a cause"
47+
\`);
48+
});
49+
"
50+
`;
51+
2152
exports[`works fine when function throws error: initial write 1`] = `
2253
"test('works fine when function throws error', () => {
2354
expect(() => {

e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,55 @@ test('works fine when function throws error', () => {
4343
}
4444
});
4545

46+
test('works fine when function throws error with cause', () => {
47+
const filename = 'works-fine-when-function-throws-error-with-cause.test.js';
48+
const template = makeTemplate(`
49+
test('works fine when function throws error', () => {
50+
expect(() => {
51+
throw new Error('apple', {
52+
cause: new Error('banana', {
53+
cause: new Error('orange')
54+
})
55+
});
56+
})
57+
.toThrowErrorMatchingInlineSnapshot();
58+
});
59+
`);
60+
61+
{
62+
writeFiles(TESTS_DIR, {[filename]: template()});
63+
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
64+
const fileAfter = readFile(filename);
65+
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
66+
expect(fileAfter).toMatchSnapshot('initial write with cause');
67+
expect(exitCode).toBe(0);
68+
}
69+
});
70+
71+
test('works fine when function throws error with string cause', () => {
72+
const filename =
73+
'works-fine-when-function-throws-error-with-string-cause.test.js';
74+
const template = makeTemplate(`
75+
test('works fine when function throws error', () => {
76+
expect(() => {
77+
throw new Error('apple', {
78+
cause: 'here is a cause'
79+
});
80+
})
81+
.toThrowErrorMatchingInlineSnapshot();
82+
});
83+
`);
84+
85+
{
86+
writeFiles(TESTS_DIR, {[filename]: template()});
87+
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
88+
const fileAfter = readFile(filename);
89+
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
90+
expect(fileAfter).toMatchSnapshot('initial write with cause');
91+
expect(exitCode).toBe(0);
92+
}
93+
});
94+
4695
test('updates existing snapshot', () => {
4796
const filename = 'updates-existing-snapshot.test.js';
4897
const template = makeTemplate(`

packages/jest-snapshot/src/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {types} from 'util';
89
import * as fs from 'graceful-fs';
910
import type {Config} from '@jest/types';
1011
import type {MatcherFunctionWithContext} from 'expect';
@@ -522,12 +523,25 @@ const _toThrowErrorMatchingSnapshot = (
522523
);
523524
}
524525

526+
let message = error.message;
527+
while ('cause' in error) {
528+
error = error.cause;
529+
if (types.isNativeError(error) || error instanceof Error) {
530+
message += `\nCause: ${error.message}`;
531+
} else {
532+
if (typeof error === 'string') {
533+
message += `\nCause: ${error}`;
534+
}
535+
break;
536+
}
537+
}
538+
525539
return _toMatchSnapshot({
526540
context,
527541
hint,
528542
inlineSnapshot,
529543
isInline,
530544
matcherName,
531-
received: error.message,
545+
received: message,
532546
});
533547
};

0 commit comments

Comments
 (0)