-
-
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
add Immutable pretty-format plugins for Immutable.OrderedSet and Immu… #2899
add Immutable pretty-format plugins for Immutable.OrderedSet and Immu… #2899
Conversation
I can add the rest of Immutable data structures once we agree this is the way to go. Thanks for reviewing this PR. |
Hm, I think it would be worth to add all Immutable collections instead of just 2. |
Also, how about calling it |
Ok I'll work on the rest of immutable data structures. |
Oh, I didn't remember that. Now after reading this thread again I'm not super sure using |
@thymikee if I remember correctly I made that exact same comment on the previous PR. We cannot use Immutable.toString here because of the problem you mentioned. |
I think the previous PR comment was about not relying on |
I don't think we should call toString and materialize the entire immutable object just to know which type it is. cc @leebyron can you help us out a bit? What's the best way to find out whether a value is from immutable.js and what type it is without explicitly requiring immutable.js? |
@cpojer I am not using |
@cpojer I am checking if an object is Immutable.List the same way immutable source code does: https://github.com/facebook/immutable-js/blob/master/src/List.js#L201 |
Codecov Report
@@ Coverage Diff @@
## master #2899 +/- ##
==========================================
+ Coverage 67.86% 68.32% +0.45%
==========================================
Files 147 155 +8
Lines 5349 5436 +87
==========================================
+ Hits 3630 3714 +84
- Misses 1719 1722 +3
Continue to review full report at Codecov.
|
@cpojer @leebyron can you take a look at this again. |
@cpenarrieta the way you test for Immutable is totally OK. It's just It's also great that you use |
@thymikee an Immutable.Set with react components doesn't look good |
@thymikee support for React components inside Immutable elements is done |
@thymikee can you look at this again? I did some refactor of the code. |
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 great so far. I've left some inline comments and thoughts
if (reactPlugin.test(item)) { | ||
result += addKey(isMap, key) + | ||
reactPlugin.print(item, print, indent, opts, colors) + ', '; | ||
} else { |
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.
We need to remove this, as we don't want Immutable plugin to be dependent on ReactElement plugin. Instead they need to cooperate nicely (which is actually done already!)
You can change this to:
val.forEach((item: any, key: any) => {
result += addKey(isMap, key) +
print(item, print, indent, opts, colors) + ', ';
});
return result + ' []'; | ||
} | ||
|
||
result += ' [ '; |
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.
There's a opts
argument in print()
which looks like this:
{ edgeSpacing: '\n', min: false, spacing: '\n' } // when {min: false}
{ edgeSpacing: '', min: true, spacing: ' ' } // when {min: true}
Can you make sure it renders nicely for both cases?
const reactComponent = React.createElement('Mouse', null, 'Hello World'); | ||
assertImmutableObject( | ||
Immutable.OrderedSet([reactComponent, reactComponent]), | ||
'Immutable.OrderedSet [ <Mouse>\n Hello World\n</Mouse> ]' |
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 this case you should include ReactElement and ReactTestComponent in plugins
it('supports multiple string elements', () => { | ||
assertImmutableObject( | ||
Immutable.OrderedSet(['jhon', 'mike', 'cristian']), | ||
'Immutable.OrderedSet [ "jhon", "mike", "cristian" ]' |
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.
For {min: true}
I'd rather print:
Immutable.OrderedSet ["jhon", "mike", "cristian"]
and for {min: false}
:
Immutable.OrderedSet [
"jhon",
"mike",
"cristian",
]
Just like it works for arrays and objects now.
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 applies to all other Immutable entities
ImmutableMapPlugin, | ||
ImmutableSetPlugin, | ||
ImmutableStackPlugin, | ||
]; |
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.
We also use plugins in jest-diff
and jest-snapshot
.
Alos, maybe we could create ImmutablePlugins.js
which exposes an array of all Immutable plugins?
@thymikee I think I solved all your observations, please take a look again. |
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.
Last 2 changes and I think we're good to go.
cc: @cpojer
assertImmutableObject( | ||
Immutable.OrderedSet(['jhon', 'mike', 'cristian']), | ||
'Immutable.OrderedSet [\n "jhon",\n "mike",\n "cristian",\n]', | ||
{min: false} |
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.
min
is set to false
by default, so it's not necessary here
packages/jest-diff/src/index.js
Outdated
@@ -30,6 +31,7 @@ const PLUGINS = [ | |||
ReactTestComponentPlugin, | |||
ReactElementPlugin, | |||
AsymmetricMatcherPlugin, | |||
...ImmutablePlugins, |
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 will fail on Node 4, as array spread is supported only behind a flag. You can use concat
.
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, looks like it works
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.
you are right, it fails in circleCI. its fixed now.
package.json
Outdated
@@ -30,7 +30,8 @@ | |||
"react-test-renderer": "^15.4.1", | |||
"rimraf": "^2.5.4", | |||
"strip-ansi": "^3.0.1", | |||
"typescript": "^2.1.4" | |||
"typescript": "^2.1.4", | |||
"immutable": "^3.8.1" |
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 insert this in the right alphabetical location please?
return result.slice(0, -2) + opts.edgeSpacing + closeTag; | ||
} else { | ||
//remove last spacing | ||
return result.slice(0, -1) + opts.edgeSpacing + closeTag; |
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.
Instead of using slice here, can we build up an array and call join(',' + opts.spacing)
down here?
|
||
module.exports = { | ||
print: printImmutableStack, | ||
test: (object: Object) => object && isStack(object), |
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.
Could we call each function in every plugin simply "print" and "test"?
That way we can do:
module.exports = {print, test};
and everything will look consistent across plugins.
Nice! cc @leebyron do you have any comments about this? :) |
const ImmutablePlugins = require('../plugins/ImmutablePlugins'); | ||
|
||
function assertImmutableObject(actual, expected, opts) { | ||
expect( |
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.
Could we use expect.extend
instead?
See http://facebook.github.io/jest/docs/expect.html#expectextendmatchers
expect(immutableValue).toPrettyPrintTo('Immutable.OrderedSet')
or something like this?
@cpojer I solved all your observations but the use of |
Looks good to me |
); | ||
const pass = prettyPrintImmutable === expected; | ||
|
||
const message = pass |
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.
wow, lovely! Thank you so much @cpenarrieta
print: Function, | ||
indent: Function, | ||
opts: Object, | ||
colors: Object |
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 flow typing here could be a lot stronger. @cpenarrieta we just added flow types to pretty-format itself – mind sending a second, follow-up PR, where you use the types from the main module? You can extract it into a types.js file like we do in other packages.
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'll work on it
thanks
Solid work, thanks @cpenarrieta! |
jestjs#2899) * add Immutable pretty-format plugins for Immutable.OrderedSet and Immutable.List * adding immutable pretty-format plugins * update yarn.lock with immutable library * adding support for react components in Immutable pretty-format plugins * adding copyright header * refactor printImmutable method * fix flow check * add support for opts {min: false} for immutable plugins * change ES2015 spread syntax to .concat * add missing semicolon * refactor printImmutable * adding trailing comma when {min: false} * refactor immutable tests using expect.extend toPrettyPrintTo
jestjs#2899) * add Immutable pretty-format plugins for Immutable.OrderedSet and Immutable.List * adding immutable pretty-format plugins * update yarn.lock with immutable library * adding support for react components in Immutable pretty-format plugins * adding copyright header * refactor printImmutable method * fix flow check * add support for opts {min: false} for immutable plugins * change ES2015 spread syntax to .concat * add missing semicolon * refactor printImmutable * adding trailing comma when {min: false} * refactor immutable tests using expect.extend toPrettyPrintTo
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. |
Comparing Immutable objects should show a more descriptive failure message
Summary
fixes #2489
I am checking if an object is
Immutable.List
the same way immutable source code does: https://github.com/facebook/immutable-js/blob/master/src/List.js#L201I am checking if an object is
Immutable.OrderedSet
the same way immutable source code does: https://github.com/facebook/immutable-js/blob/master/src/OrderedSet.js#L44Test plan
Testing it from an external project
I also created unit test to test the new plugins