-
-
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
feat: expose internal matchers on extended matchers context #13375
feat: expose internal matchers on extended matchers context #13375
Conversation
This reverts commit 3d623a3.
Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
Can you add an integration test with some snapshots showing what the errors look like if you use the matchers inside your own? |
Sure! |
Few more details regarding types. (I had to look through to remember how it all works.) Take a look at TypeScript tab in I am trying to say that you added typings of new prop directly to Another tricky part – Just wondering – did you consider simply adding |
I did consider just adding to the MatcherUtils, but if I do that the internals matchers would have access as well, for me at least don't make much sense to have that's why I thought creating a different type for the functions created inside extend so they have a different context. I agree that it would be much more straightforward just to add to the MatcherUtils instead, what do you think would be a better solution? |
I don't mind internal matchers having access. Probably easier to have some |
Sorry for the delay. I've made the changes you guys asked. |
Would be nice to have a line in documentation; and to add a type test to packages/expect/__typetests__/expect.test.ts. To run the type tests: build the repo and run |
I'm thinking this is fine as is and we can make the types better in a future patch. Unless you're currently working on the types @JVBorges? |
a5b89d1
to
b7bf046
Compare
|
||
expect.extend({ | ||
toPass(received, expected) { | ||
const {pass} = this.matchers.toEqual(received, expected); |
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.
to make this test pass I have to do this.matchers.toEqual.call(this, received, expected);
here - i.e. explicitly pass in this
. any idea what changed to make this test now 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.
no clue but I'll take a look
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.
Solved this by binding the context to the matchers when I expose them, but doing this way the unit testing started failing.
Later I will try to fix them
Not yet, but I was going to check it out this weekend. |
Cool, thanks! 👍 |
So I made this change where I bind the context to every matcher before exposing them but this caused the unit test to fail. Before, the test passed because they were the same object so when compared with Object.is returned true Since now the exposed matchers have been mutated, Object.is start failing thus the algorithm starts to check each key and fails at this line: https://github.com/facebook/jest/blob/bed4891b0e5b614988e5a65fca29c4734cdb3c2e/packages/expect-utils/src/jasmineUtils.ts#L129-L131 I'm not sure how I'll do this unit test now. Do you guys have any clue? |
@mrazauskas @SimenB any suggestions? |
Unfortunately I have nothing to suggest at this moment. |
This PR is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
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 #10590
Summary
The idea is to have access to the internal matchers Jest has when creating custom matchers. The internal matchers are only exposed to those functions created from expect.extend() function.
Also, I moved the CustomMatcher class definition to the asymmetricMatchers file. Since that class is only being used when defining the custom matcher as asymmetric in the expect object, I guess it makes more sense to have it over there.
Test plan
Tests have been added to ensure internal matchers are exposed in the custom matcher context