Skip to content
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

Merged
merged 14 commits into from
Jan 15, 2017
Merged

Conversation

thymikee
Copy link
Collaborator

@thymikee thymikee commented Dec 30, 2016

Summary

  • Provide a basic proof of concept
  • Extract asymmetric-matchers: any, anything, arrayContaining, objectContaining, stringMatching from jasmine-utils into separate module
  • Rename <jasmine.asymmetricMatcherName> to just <asymmetricMatcherName>
  • Refactor asymmetric-matchers
  • Improve Flow coverage
  • Create AsymmetricMatchers plugin for pretty-format
  • Use the plugin instead of basic proof of concept
  • Tests

Fixes #2397

@codecov-io
Copy link

codecov-io commented Dec 30, 2016

Current coverage is 67.43% (diff: 97.40%)

Merging #2476 into master will increase coverage by 2.60%

@@             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          

Powered by Codecov. Last update fa39ef8...fd7a7ec

@thymikee thymikee changed the title [WIP] Pretty-print asymmetric matchers Pretty-print asymmetric matchers Jan 2, 2017
@thymikee thymikee requested a review from cpojer January 2, 2017 17:40
@thymikee
Copy link
Collaborator Author

thymikee commented Jan 2, 2017

Added tests for asymmetric-matchers, as they are our responsibility now.

@jest-bot
Copy link
Contributor

jest-bot commented Jan 10, 2017

Warnings
⚠️ Please ensure that @flow is enabled on: packages/jest-matchers/src/__tests__/asymmetric-matchers-test.js and packages/pretty-format/src/__tests__/AsymmetricMatcher-test.js

Generated by 🚫 dangerJS

@thymikee
Copy link
Collaborator Author

Here's a preview of how it looks.

screen shot 2017-01-12 at 12 49 00

screen shot 2017-01-12 at 12 49 11


asymmetricMatch(other: Array<any>) {
const className = Object.prototype.toString.call(this.sample);
if (className !== '[object Array]') {
Copy link
Member

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>';
Copy link
Member

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.

Copy link
Member

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');
Copy link
Member

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.

Copy link
Collaborator Author

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...

Copy link
Member

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.

@cpojer
Copy link
Member

cpojer commented Jan 12, 2017

I'm a bit unsure about the printing here. Could we simplify it and get rid of what Jasmine does? How about:

ArrayContaining [

]
ObjectContaining {

}

and similar. What do you think?

@cpojer
Copy link
Member

cpojer commented Jan 12, 2017

Overall this looks really great!

@thymikee
Copy link
Collaborator Author

yes!!!!! ❤️
This should also look good on diffs

@thymikee
Copy link
Collaborator Author

I'll iterate over the PR sometime today

}

toAsymmetricMatcher(print: Function) {
return 'ArrayContaining ' + print(this.sample).replace(/Array\s+/, '');
Copy link
Member

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.

Copy link
Member

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.

Copy link
Collaborator Author

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

Copy link
Member

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.

@cpojer
Copy link
Member

cpojer commented Jan 15, 2017

Alright, I think if we can improve how we print this, this is ready to go.

@thymikee
Copy link
Collaborator Author

Rendering of ArrayContaining, ObjectContaining and StringMatching is now moved to pretty-format plugin, without "regexping" anything.

@cpojer cpojer merged commit 07b4bf7 into jestjs:master Jan 15, 2017
@cpojer
Copy link
Member

cpojer commented Jan 15, 2017

Stellar work! Thanks for sticking with me through the iterations.

@thymikee thymikee deleted the fix/asymmetric-matchers branch January 15, 2017 21:39
skovhus pushed a commit to skovhus/jest that referenced this pull request Apr 29, 2017
* 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
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* 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
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix pretty printing of asymmetric matchers.
5 participants