-
-
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
Ability to create asymmetric matchers that are recognized by jest. #4711
Comments
I'm open to this :) |
Although it is implied but not currently documented, Jest assertions evaluate asymmetric matcher objects as defined in Jasmine: https://jasmine.github.io/edge/introduction#section-Custom_asymmetric_equality_tester Yes, as you suggest, if the assertion fails, the message needs to display an expected value better than @mayank23 Here are some files to study, especially as you plan ahead for tests to add:
When I studied the code to answer your question about
@cpojer Can you confirm or adjust this direction:
|
@pedrottimark yep! Sounds great. |
@pedrottimark @cpojer Awesome, thanks for the information! Will start on this soon. |
@mayank23 Super. Can you link from a comment in this issue to a gist of some examples of asymmetric matchers with received and expected values so an assertion would fail? That will help me double-check any improvement to interface between matcher instances and plugin to print expected value so EDIT |
Hi @pedrottimark , here's my gist of 3 examples. https://gist.github.com/mayank23/4a5ac7a7cbb745aefc995777519338f5 I listed what I think would be nice to have printed when a match fails in each Asymmetric Matcher's getAsymmetricMatcher method. Technically, this should run if all files were in a single directory, I commented the main use case for each too. The main test cases file: https://gist.github.com/mayank23/4a5ac7a7cbb745aefc995777519338f5#file-testcases-js. Each test case name is denoted with the prefix 'PASS:' and 'FAIL:' for the passing and failing test cases respectively. Thanks! |
Hi @pedrottimark did you have any questions regarding the examples I links? Thanks! |
@mayank23 You gave just what I need to stretch my own thinking. Thank you very much. This issue will be in my mental slow cooker until Friday. In case this helps to stretch your thinking about the developer experience we are aiming for, here is a picture of results from
|
EDITED AGAIN on 2017-10-28: To explore the space of possible solutions:
|
Something out of scope for this issue, but that we want not to make harder to do in the future: In some cases like picture in preceding comment, results from |
@thymikee @mayank23 First draft of contract between matchers and plugin for y’all to critique: EDITED on 2017-10-25 For plugin to get name of matcher:
EDITED on 2017-10-28 For plugin to serialize matcher:
EDITED: serialize(/* ignores its arguments in this example */) {
return 'any(' + fnNameFor(this.sample) + ')';
}
Withdraw special case for arg of 2 built-in matchers because cannot do it for application-specific. arrayContaining(Array [
"Alice",
"Bob",
])
objectContaining(Object {
"x": any(Number),
"y": any(Number),
}) Hypothetical examples from gist linked in earlier comment:
|
@thymikee Indirectly related to this issue, if Jest raises awareness of built-in asymmetric matchers so people (like me :) study or even copy-and-edit to make application-specific matchers, can we converge on one pattern for where matchers throw error about invalid sample? 3 in
2 in
EDITED on 2017-10-28 to see developer experience from-the-outside-in: For
For
Therefore, throw error about args in constructor so line number points to source of problem? The |
@mayank23 After thinking about this at turtle pace, here is hint where to make changes:
@cpojer @thymikee What do you think about adding To get
// EDITED to add `getExpectedType` and replace ES2015 arrow functions with functions :)
const myMatcher = Object.assign(new AsymmetricMatcher(), {
asymmetricMatch: function (received) { return received % modulus === 0; },
getExpectedType: function () { return 'number'; },
getName: function () { return 'modulusMatcher'; },
getArgs: function () { return [modulus]; },
}); |
I'm good with exposing |
Alright, will get started on this soon. Thanks! |
@mayank23 Super! To keep a short feedback loop of review, I suggest:
While you work on code, would you like me to think about docs? |
awesome, thanks for the advice! yes that would be great! |
@mayank23 Happy New Year! Do you still want to make a pull request? Is it possible in January? |
Hi @pedrottimark Happy new year to you too! Yes I will try to get this in this month. Sorry about the delay |
I'm a total newbie but interested in helping if someone guides me around! |
@aymericbouzy Welcome to Jest! I will read your recent issue and review this issue on Friday. Here are some first steps:
Bonus: If you find anything in |
Hi @pedrottimark! Thanks 😃 I've done all that alright 👍 Not exactly sure what you mean by Workflow, but I've run both commands successfully. I must say the experience of opening the repo in VS Code and having the recommended extensions showing up is impressive 🤩 Especially the Jest extension looks incredible ! |
mh that's weird, when jest extension runs from VS Code, there are a lot of problems with the test snapshots, it says almost all of them have changed 🤔 |
Can we reopen this issue? I don't think the issue is being addressed. We still cannot provide a good error message other than
when our custom asymmetric matcher returns false |
Please open up a new issue with a reproduction. Thanks! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hi!
I would like to be able to create asymmetric matchers using an api exported from jest which can automatically insert :
$$typeof = Symbol.for('jest.asymmetricMatcher')
as a property of the asymmetric matcher.In turn, I would be able to get support from the
pretty-format
asymmetric_matcher
plugin, and it would be able to print the result of callingmyAsymmetricMatcher.toAsymmetricMatcher()
for the diff string.Is this a feature the Jest team would like incorporate?
I can work on a PR if someone could hint at the right place on where to make the changes. I was thinking of adding this jest asymmetric matcher factory possibly in the
jest-matcher-utils
. Would this be fine?Thank you!
The text was updated successfully, but these errors were encountered: