-
-
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
[Discussion] Expect only overrides error stack for built-in matchers #5162
[Discussion] Expect only overrides error stack for built-in matchers #5162
Conversation
f438ca8
to
fe5ca1f
Compare
fe5ca1f
to
f39d750
Compare
Codecov Report
@@ Coverage Diff @@
## master #5162 +/- ##
=======================================
Coverage 61.18% 61.18%
=======================================
Files 202 202
Lines 6765 6765
Branches 3 4 +1
=======================================
Hits 4139 4139
Misses 2625 2625
Partials 1 1 Continue to review full report at Codecov.
|
@@ -35,6 +35,13 @@ export const setState = (state: Object) => { | |||
|
|||
export const getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers; | |||
|
|||
export const setMatchers = (matchers: MatchersObject) => { | |||
export const setMatchers = (matchers: MatchersObject, isInternal: boolean) => { | |||
for (const key in matchers) { |
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.
In the spirit of babel/babel#6748 can we change it to forEach
?
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.
Interesting. Sure thing!
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.
Can you include the integration test from the other PR as well? If we regress, it's easier to identify than if we just get a different number i the trace in the tests in this PR
packages/expect/src/index.js
Outdated
@@ -296,4 +298,8 @@ expect.getState = getState; | |||
expect.setState = setState; | |||
expect.extractExpectedAssertionsErrors = extractExpectedAssertionsErrors; | |||
|
|||
// Expose JestAssertionError for custom matchers | |||
// This enables them to preserve the stack for specific errors | |||
expect.JestAssertionError = JestAssertionError; |
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.
Is this still needed in this case?
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.
Nope! Removed. 😄
I'm not clear on how that integration test improves clarity in this case 😄 but I've re-added it! |
|
||
exports[`Custom matcher preserves error stack 1`] = ` | ||
"Error: qux | ||
at baz (/Users/bvaughn/Documents/git/jest/integration_tests/__tests__/custom_matcher.test.js:49:13) |
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.
this shouldn't happen, right? we should still clean it up to be relative paths (especially for this integration test, but in general as well)
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.
Or do we really not want prettified stack trace at all?
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.
😆 Sorry. This was super silly of me. I'm embarrassed. I noticed that CI failed but hadn't had a chance yet to circle back and see why.
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.
I'm not sure if I fixed this in the best way 😄 but it now tests the prettified, relative stack.
@bvaughn I added a commit with what I meant, and it seems like it doesn't work properly - the stack is lost. I haven't dug into why yet, ideas? |
My thinking was that then we can see from the outside that the stack is preserved when running all of jest, instead of just |
53 | }); | ||
54 | | ||
|
||
at __tests__/custom_matcher.test.js:51:8 |
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.
this is wrong, we have lost the trace
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.
Yes, as of your commit.
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.
Indeed. should be fixed now
I don't really know Jest's infrastructure well enough to understand the implications of your commit.
It did work until it was moved. 😄 I'm not sure if the fact that it doesn't work now indicates a problem with the test, or the way it's now being run. It looks like the test has been changed slightly to verify the output of |
No, I'm being stupid. My watch process had crashed, so I didn't have your code changes compiled when running jest. It does indeed work :D Sorry about that |
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.
I still think it's sorta weird how we mess with the stacks, but this is a move in the right direction feature wise I think
😆 No worries at all. I was pretty confused. |
Thank you for shepherding me through my first commit to Jest, @SimenB 🙇 |
Gah, Windows... 🙁 I'll try to skip it, but might have to move the test to a separate file so we kan sip the whole suite (because of the snapshot) |
expect(() => 'foo').toCustomMatch('foo'); | ||
}); | ||
|
||
// This test is expected to fail |
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.
it passes, is that an issue?
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.
Ah, my comment is perhaps confusing.
The "test" isn't expected to fail. The custom matcher assertion is expected to fail, which is why it's wrapped in an expect-to-throw check.
I'll update the comment. 😄
I've updated the inline comments for (hopefully) better clarity. |
export const setMatchers = (matchers: MatchersObject, isInternal: boolean) => { | ||
Object.keys(matchers).forEach(key => { | ||
const matcher = matchers[key]; | ||
Object.defineProperty(matcher, '__jestInternal', { |
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.
Can we not use a Symbol instead to make this actually secret?
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.
Done!
This is looking good, thanks for sticking with us. My only two concerns are:
|
also format all markdown files
Thanks @cpojer!
Yes. Good suggestion.
I chose this approach for a couple of reasons:
|
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.
This works for me. Let's make CI green and then feel free to merge it :)
Ah! The test that was failing CI ( |
const foo = () => bar(); | ||
const bar = () => baz(); | ||
const baz = () => { | ||
throw Error('qux'); |
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.
Should this be throw new Error
? I'm not sure if there's a semantical difference...
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.
No difference that I'm aware of.
15.11.1 The Error Constructor Called as a Function
When
Error
is called as a function rather than as a constructor, it creates and initialises a newError
object. Thus the function callError(…)
is equivalent to the object creation expressionnew Error(…)
with the same arguments.
source: http://ecma-international.org/ecma-262/5.1/#sec-15.11.1
Awesome, thanks for working on this! |
Thank you and @SimenB for your help! |
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. |
Proof of concept for Christoph's suggestion from PR #5138.
Resolves #5136
Note: I don't have much context for the Jest project so this might be an unacceptable hack. 😄