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

Add support for Immutable.Record in pretty-format #3678

Merged
merged 2 commits into from
May 30, 2017

Conversation

renchap
Copy link
Contributor

@renchap renchap commented May 28, 2017

Summary

Immutable.Record are not handled by pretty-print, but other Immutable.js structures are. Record are often used in Redux states for example, and Redux reducers tested using snapshots. This will allow the snapshots to be more precise, referencing proper Immutable.Record and not plain JS objects.

Test plan

I added tests for the various usecases I had in mind.

This fixes #3677

@thymikee can you review it? I tried to not add anything Record-specific in lib/printImmutable.js and rely on val._keys and val._name.

val._name is only used for Record, but Immutable.Seq (which is not supported by pretty-print) also has ._keys. Maybe I should check if val.forEach is defined and fallback to val._keys? Or should I specificaly check if a record is passed?

@renchap renchap force-pushed the pretty-format-immutable-record branch from 6f681c6 to e782cbc Compare May 28, 2017 21:58
@codecov-io
Copy link

codecov-io commented May 28, 2017

Codecov Report

Merging #3678 into master will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3678      +/-   ##
==========================================
+ Coverage   62.57%   62.63%   +0.05%     
==========================================
  Files         181      182       +1     
  Lines        6694     6704      +10     
  Branches        6        6              
==========================================
+ Hits         4189     4199      +10     
  Misses       2502     2502              
  Partials        3        3
Impacted Files Coverage Δ
...ages/pretty-format/src/plugins/ImmutablePlugins.js 100% <ø> (ø) ⬆️
...es/pretty-format/src/plugins/lib/printImmutable.js 100% <100%> (ø) ⬆️
...kages/pretty-format/src/plugins/ImmutableRecord.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f6f7899...854738f. Read the comment docs.

immutableArray.push(
indent(addKey(isMap, key) + print(item, print, indent, opts, colors)),
),
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract a function and reuse it? Something like:

const pushToImmutableArray = (item: any, key: string) => {
  immutableArray.push(
    indent(addKey(isMap, key) + print(item, print, indent, opts, colors)),
  )
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍!
@cpojer anything to add?


if (Array.isArray(val._keys)) {
// if we have a record, we can not iterate on it directly
val._keys.forEach(key => pushToImmutableArray(val.get(key), key));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are looking at private fields here and with ._name above. I'm worried about changes in Immutable that will break this and therefore break snapshots.

cc @leebyron do you have any input for us on what we should do here? Do you think this is fine for pretty-printing?

Copy link
Contributor

@leebyron leebyron May 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine as long as someone is prepared to update this code if Immutable.js implementation details ever change, which is entirely possible. Though I think there are options for improvements

For ._name, you might want to sniff for Immutable v4.0 which offers Record.getDescriptiveName(record), otherwise use this fallback implementation, however note that your implementation differs from Immutable.js's.

I think ._keys is less safe to use. Since you just want to iterate over these, you can probably cast to a lazy Seq first. So lines 48-53 here can be replaced with:

val.toSeq().forEach(pushToImmutableArray)

@cpojer cpojer merged commit d7702f3 into jestjs:master May 30, 2017
@cpojer
Copy link
Member

cpojer commented May 30, 2017

I merged this even though it currently accesses some private fields. We already do duck-typing for the immutable types and that could change any time, the private fields aren't any different. However, it would be great if immutable had an official introspection API for debugging. If it does, or if it gets added, we should switch to that asap.

@renchap
Copy link
Contributor Author

renchap commented May 30, 2017

There is an introspective API, for example:

I used private fields in my patch to be consistent with the original implementation. From my quick check, the only thing that is not accessible is the list of attributs in a Record (where I am using record._keys). I am not sure why this has been implemented with duck-typing and I have not been able to find any reason in #2899.
Maybe it is to avoid loading Immutable.js code for projects that are not using the library?

@renchap renchap deleted the pretty-format-immutable-record branch May 30, 2017 10:53
@cpojer
Copy link
Member

cpojer commented May 30, 2017

yep, you are right. We are trying not to require immutable at all. If the introspective API was available through objects from immutable so we can avoid requiring immutable (or any one version of immutable) directly, then we can use them.

tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Add support for Immutable.Record in pretty-format

Fixes jestjs#3677

* Extract pushToImmutableArray function
@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 13, 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.

pretty-format does not support Immutable.Record
6 participants