-
-
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
Pretty-print asymmetric matchers #2476
Conversation
Current coverage is 67.43% (diff: 97.40%)@@ master #2476 diff @@
==========================================
Files 125 125
Lines 4871 4889 +18
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 3158 3297 +139
+ Misses 1713 1592 -121
Partials 0 0
|
4cd3f63
to
0a27cda
Compare
Added tests for asymmetric-matchers, as they are our responsibility now. |
b8cf138
to
538fb18
Compare
Generated by 🚫 dangerJS |
|
||
asymmetricMatch(other: Array<any>) { | ||
const className = Object.prototype.toString.call(this.sample); | ||
if (className !== '[object Array]') { |
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 do Array.isArray
.
@@ -276,102 +258,12 @@ function fnNameFor(func) { | |||
return func.name; | |||
} | |||
|
|||
var matches = func.toString().match(/^\s*function\s*(\w*)\s*\(/); | |||
const matches = func.toString().match(/^\s*function\s*(\w*)\s*\(/); | |||
return matches ? matches[1] : '<anonymous>'; |
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.
did you mean to the fnNameFor function here? It's in the other file too.
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.
oh, oops, nevermind.
|
||
const prettyFormat = require('../'); | ||
const AsymmetricMatcher = require('../plugins/AsymmetricMatcher'); | ||
const jestExpect = require('jest-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.
not really sure why this works. You need to make jest-matchers
a devDependency of pretty-format.
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.
BTW, do I really need jestExpect
here instead of available expect
? I mean this is test for pretty-format, not for a matcher...
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.
Try it! If it works, that should be fine here.
I'm a bit unsure about the printing here. Could we simplify it and get rid of what Jasmine does? How about:
and similar. What do you think? |
Overall this looks really great! |
yes!!!!! ❤️ |
I'll iterate over the PR sometime today |
} | ||
|
||
toAsymmetricMatcher(print: Function) { | ||
return 'ArrayContaining ' + print(this.sample).replace(/Array\s+/, ''); |
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.
Won't this get rid of all sub arrays? I'm a bit worried this isn't going to be stable and break in subtle ways and when I made the suggestion, I didn't know how to work around this. Another idea would be to say:
Contains Array [
1,
2,
]
that way you only need to prepend parts of the string.
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.
The same goes for all the other .replace
calls in this file. Ideally I'd like to find a way without this.
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 safer. But with this regex I don't pass g
flag, so it only replaces first occurrence. Tested this on real examples, but I could add an extra test for 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 think we print the name of constructors, so we could theoretically do something like this:
class ArrayContaining extends Array {}
const array = new ArrayContaining();
array.push(...this.sample);
return array; // pretty format this.
that may work! In ES2015+ you can subclass and extend arrays.
Alright, I think if we can improve how we print this, this is ready to go. |
Rendering of ArrayContaining, ObjectContaining and StringMatching is now moved to |
Stellar work! Thanks for sticking with me through the iterations. |
* First attempt to fix this * Extract asymmetric-matchers from jasmine-utils * Remove <jasmine.*> adnotations * Remove obsolete snapshots * Add AsymmetricMatcher plugin for pretty-format * Refactor asymmetric-matchers * Use AsymmetricMatcher plugin in diffs * Remove unused prettyFormat from asymmetric-matcher * Check for object presence in AsymmetricMatcher * Update snapshots * Tests for asymmetric-matchers * Update snapshots with patch marks * Adjust printing of asymmetric matchers * Use class instances instead of regex to render values
* First attempt to fix this * Extract asymmetric-matchers from jasmine-utils * Remove <jasmine.*> adnotations * Remove obsolete snapshots * Add AsymmetricMatcher plugin for pretty-format * Refactor asymmetric-matchers * Use AsymmetricMatcher plugin in diffs * Remove unused prettyFormat from asymmetric-matcher * Check for object presence in AsymmetricMatcher * Update snapshots * Tests for asymmetric-matchers * Update snapshots with patch marks * Adjust printing of asymmetric matchers * Use class instances instead of regex to render values
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
any
,anything
,arrayContaining
,objectContaining
,stringMatching
fromjasmine-utils
into separate module<jasmine.asymmetricMatcherName>
to just<asymmetricMatcherName>
asymmetric-matchers
AsymmetricMatchers
plugin forpretty-format
Fixes #2397