Skip to content

Commit

Permalink
feat(jest-snapshot): Support Error.cause in snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Feb 28, 2023
1 parent bb39cb2 commit 338be2a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@ exports[`updates existing snapshot: updated snapshot 1`] = `
"
`;
exports[`works fine when function throws error with cause: initial write with cause 1`] = `
"test('works fine when function throws error', () => {
expect(() => {
throw new Error('apple', {
cause: new Error('banana', {
cause: new Error('orange'),
}),
});
}).toThrowErrorMatchingInlineSnapshot(\`
"apple
Cause: banana
Cause: orange"
\`);
});
"
`;
exports[`works fine when function throws error with string cause: initial write with cause 1`] = `
"test('works fine when function throws error', () => {
expect(() => {
throw new Error('apple', {
cause: 'here is a cause',
});
}).toThrowErrorMatchingInlineSnapshot(\`
"apple
Cause: here is a cause"
\`);
});
"
`;
exports[`works fine when function throws error: initial write 1`] = `
"test('works fine when function throws error', () => {
expect(() => {
Expand Down
49 changes: 49 additions & 0 deletions e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,55 @@ test('works fine when function throws error', () => {
}
});

test('works fine when function throws error with cause', () => {
const filename = 'works-fine-when-function-throws-error-with-cause.test.js';
const template = makeTemplate(`
test('works fine when function throws error', () => {
expect(() => {
throw new Error('apple', {
cause: new Error('banana', {
cause: new Error('orange')
})
});
})
.toThrowErrorMatchingInlineSnapshot();
});
`);

{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
const fileAfter = readFile(filename);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(fileAfter).toMatchSnapshot('initial write with cause');
expect(exitCode).toBe(0);
}
});

test('works fine when function throws error with string cause', () => {
const filename =
'works-fine-when-function-throws-error-with-string-cause.test.js';
const template = makeTemplate(`
test('works fine when function throws error', () => {
expect(() => {
throw new Error('apple', {
cause: 'here is a cause'
});
})
.toThrowErrorMatchingInlineSnapshot();
});
`);

{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
const fileAfter = readFile(filename);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(fileAfter).toMatchSnapshot('initial write with cause');
expect(exitCode).toBe(0);
}
});

test('updates existing snapshot', () => {
const filename = 'updates-existing-snapshot.test.js';
const template = makeTemplate(`
Expand Down
16 changes: 15 additions & 1 deletion packages/jest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {types} from 'util';
import * as fs from 'graceful-fs';
import type {Config} from '@jest/types';
import type {MatcherFunctionWithContext} from 'expect';
Expand Down Expand Up @@ -522,12 +523,25 @@ const _toThrowErrorMatchingSnapshot = (
);
}

let message = error.message;
while ('cause' in error) {
error = error.cause;
if (types.isNativeError(error) || error instanceof Error) {
message += `\nCause: ${error.message}`;
} else {
if (typeof error === 'string') {
message += `\nCause: ${error}`;
}
break;
}
}

return _toMatchSnapshot({
context,
hint,
inlineSnapshot,
isInline,
matcherName,
received: error.message,
received: message,
});
};

0 comments on commit 338be2a

Please sign in to comment.