-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix stack trace for errors thrown by snapshot serializers #4787
Changes from 2 commits
63e1795
d523a22
7c7069c
ee00b4e
c0d78a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,12 +197,14 @@ const makeThrowingMatcher = ( | |
try { | ||
result = matcher.apply(matcherContext, [actual].concat(args)); | ||
} catch (error) { | ||
if (!(error instanceof JestAssertionError)) { | ||
// Try to remove this and deeper functions from the stack trace frame. | ||
if ( | ||
!(error instanceof JestAssertionError) && | ||
error.name != 'PrettyFormatPluginError' && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Triple equals? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seeing as the linter doesn't complain, maybe not needed |
||
// Guard for some environments (browsers) that do not support this feature. | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(error, throwingMatcher); | ||
} | ||
Error.captureStackTrace | ||
) { | ||
// Try to remove this and deeper functions from the stack trace frame. | ||
Error.captureStackTrace(error, throwingMatcher); | ||
} | ||
throw error; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -549,6 +549,63 @@ describe('prettyFormat()', () => { | |
}).toThrow(); | ||
}); | ||
|
||
it('throws PrettyFormatPluginError if test throws an error', () => { | ||
const options = { | ||
plugins: [ | ||
{ | ||
print: () => '', | ||
test() { | ||
throw new Error('Where is the error?'); | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
try { | ||
prettyFormat('', options); | ||
} catch (error) { | ||
expect(error.name).toBe('PrettyFormatPluginError'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If for any reason the
let name;
try {
prettyFormat('', options);
} catch (error) {
name = error.name;
}
expect(name).toBe('PrettyFormatPluginError'); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SimenB Thank you! Because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. Or |
||
}); | ||
|
||
it('throws PrettyFormatPluginError if print throws an error', () => { | ||
const options = { | ||
plugins: [ | ||
{ | ||
print: () => { | ||
throw new Error('Where is the error?'); | ||
}, | ||
test: () => true, | ||
}, | ||
], | ||
}; | ||
|
||
try { | ||
prettyFormat('', options); | ||
} catch (error) { | ||
expect(error.name).toBe('PrettyFormatPluginError'); | ||
} | ||
}); | ||
|
||
it('throws PrettyFormatPluginError if serialize throws an error', () => { | ||
const options = { | ||
plugins: [ | ||
{ | ||
serialize: () => { | ||
throw new Error('Where is the error?'); | ||
}, | ||
test: () => true, | ||
}, | ||
], | ||
}; | ||
|
||
try { | ||
prettyFormat('', options); | ||
} catch (error) { | ||
expect(error.name).toBe('PrettyFormatPluginError'); | ||
} | ||
}); | ||
|
||
it('supports plugins with deeply nested arrays (#24)', () => { | ||
const val = [[1, 2], [3, 4]]; | ||
expect( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing link to PR