Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/jest-snapshot/src/__tests__/matcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ it(`matcher returns matcher name, expected and actual values`, () => {
}),
);
});

it(`throws the error stack when match throws an error`, () => {
const error = new Error();

const matcher = toMatchSnapshot.bind({
snapshotState: {
match: () => {
throw error;
},
},
});

expect(() => matcher()).toThrow(error.stack);
});
19 changes: 13 additions & 6 deletions packages/jest-snapshot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,19 @@ const toMatchSnapshot = function(received: any, testName?: string) {
throw new Error('Jest: snapshot state must be initialized.');
}

const result = snapshotState.match(
testName && currentTestName
? `${currentTestName}: ${testName}`
: currentTestName || '',
received,
);
let result;

try {
result = snapshotState.match(
testName && currentTestName
? `${currentTestName}: ${testName}`
: currentTestName || '',
received,
);
} catch (error) {
throw new Error(error.stack);
}

const {count, pass} = result;
let {actual, expected} = result;

Expand Down