-
-
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
Expose JestAssertionError to custom matchers #5138
Conversation
Codecov Report
@@ Coverage Diff @@
## master #5138 +/- ##
======================================
Coverage 60.7% 60.7%
======================================
Files 201 201
Lines 6691 6691
Branches 4 4
======================================
Hits 4062 4062
Misses 2628 2628
Partials 1 1 Continue to review full report at Codecov.
|
It might be a bit overkill, but could you add an integration test showing that extending this error ends up with the correct trace? More as a guard for future regressions than as proof of this change working wonders. |
Yeah, definitely. I added one to |
Thinking about this on the bus, I wonder if https://github.com/facebook/jest/blob/78184f26c3fea0ba85b95ec3aed484a7a2d4d44b/packages/expect/src/index.js#L216-L225 is just completely wrong... (And that #4787 was the wrong approach) If it's not an assertion error, I think we should just wrap the error (using something like https://www.npmjs.com/package/verror), not override its |
That's sort of what my comment in the PR description, about it being "unclear to me if the current Jest behavior is the right default", referred to. But I didn't have the time to trace back through past PRs to understand why the current behavior exists. Having written many Jest tests, as well as several matchers, I am confident though that the behavior of clobbering the original error stack is- at least in some cases- definitely harmful to the end user, since it masks the source of an error and requires more effort to identify and fix. If Jest, by default, preserved the original stack (eg something like a "caused by" snippet with the original stack) that would be even better and would make this PR unnecessary. |
Could we change it to only have this behavior for Jest’s own inbuilt marchers? For those, we don’t want the deep stack traces to show up because they aren’t adding value. For any third-party marchers I suggest not cleaning the stack trace like this. |
I think this sounds like a better default behavior, at least from the perspective of an outsider. Not sure how simple it would be to implement though. I'll take a look. One thing I'm not clear on: Is the stack-swallowing behavior limited to custom matchers? Are there any built-in matchers that might also override the meaningful stack with their own? I think this would only apply for the to-throw matchers, which seem to work correctly. |
Regarding the above suggestion, I pushed PR #5162 for discussion purposes. ^ |
Closing in favor of the other 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. |
Resolves #5136
Expose
JestAssertionError
to custom matchers asexpect.JestAssertionError
. The motivation for this is described in more detail in issue #5136, along with alternate solutions.It's unclear to me if the current Jest behavior is the right default. (I don't have sufficient context to have an opinion about this.) I assume for now that it is, and so propose the smallest change to enable custom matchers to throw errors that will not have the stack overridden.
I didn't update website/documentation yet since I'm unsure if this PR will be accepted. I would be happy to make that change as well though.
Summary
With this change, it is possible to write custom matchers that preserve the original error stack, eg:
Running the above test would result in the following output:
Test plan
Above results are shown for a custom matcher I've written locally that uses this newly-exposed property. I would be happy to contribute automated test(s) as well if it's desired.