Fix stack trace for errors thrown by snapshot serializers#4787
Fix stack trace for errors thrown by snapshot serializers#4787cpojer merged 5 commits intojestjs:masterfrom nicolasiensen:fix-stack-trace
Conversation
In this commit, we are creating a new custom error called `PrettyFormatPluginError`, this error is thrown by `prettyFormat` function when a serializer throws an error. In the `expect` module, we skip `Error.captureStackTrace` if the error name is `PrettyFormatPluginError`, this way the stack trace stays intact. Fixes #3302
|
I like this. Mind adding a note in the changelog? |
|
Yeah this looks nice. Good job @nicolasiensen |
| prettyFormat('', options); | ||
| } catch (error) { | ||
| expect(error.name).toBe('PrettyFormatPluginError'); | ||
| } |
There was a problem hiding this comment.
If for any reason the prettyFormat call doesn’t throw, then the test passes?
What do you think about: DISREGARD FOLLOWING CODE
let name;
try {
prettyFormat('', options);
} catch (error) {
name = error.name;
}
expect(name).toBe('PrettyFormatPluginError');There was a problem hiding this comment.
Either do expect().toThrow() or expect.hasAssertions instead
There was a problem hiding this comment.
@SimenB Thank you! Because toThrow matches the message but the name is the test criterion, then our requested change is keep try-catch as originally written and insert expect.hasAssertions() at beginning of each of the 3 test blocks?
There was a problem hiding this comment.
True. Or .toThrow(PrettyFormatPluginError) if it's exported.
pedrottimark
left a comment
There was a problem hiding this comment.
Very nice. One suggestion about the 3 added tests.
packages/expect/src/index.js
Outdated
| // 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.
Seeing as the linter doesn't complain, maybe not needed
CHANGELOG.md
Outdated
| * `[jest-resolve]` Preserve module identity for symlinks ([#4761](https://github.com/facebook/jest/pull/4761)) | ||
| * `[jest-config]` Include error message for `preset` json ([#4766](https://github.com/facebook/jest/pull/4766)) | ||
| * `[pretty-format]` Throw `PrettyFormatPluginError` if a plugin halts with an exception ([#4787](https://github.com/facebook/jest/pull/4787)) | ||
| * `[expect]` Keep the stack trace unchanged when `PrettyFormatPluginError` is thrown by pretty-format |
|
Thank you everyone for working on this PR :) |
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
In this commit, we are creating a new custom error called
PrettyFormatPluginError, this error is thrown byprettyFormatfunction when a serializer throws an error.In the
expectmodule, we skipError.captureStackTraceif the error name isPrettyFormatPluginError, this way the stack trace stays intact.Fixes #3302
Test plan
Given a test file
foo.test.js:And a custom snapshot serializer
my-serializer-module.jsAnd a
package.json:{ "jest": { "snapshotSerializers": ["./my-serializer-module"] } }When
jestis run, with the current implementation, the failure message has no trace of the error thrown by the custom serializer:With the new implementation we have a failure message that includes a trace from the error thrown by the custom serializer: