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

Replace print with serialize in Immutable plugins #4189

Merged
merged 10 commits into from
Aug 6, 2017

Conversation

pedrottimark
Copy link
Contributor

@pedrottimark pedrottimark commented Aug 3, 2017

Summary

@cpenarrieta your feedback is welcome :) for more info, see #4114 and #4184

Breaking change in Map, OrderedMap, and Record to print the key:

  • because implicit conversion to string throws an error if key is symbol
  • because implicit conversion to string is [object Object] if key is object
  • to distinguish primitive values false (and so on) from "false" as string

Breaking changes for edge cases:

  • support maxDepth option
  • support indentation of immutable collection as child of non-immutable (the closing punctuation was incorrect in the same way as for closing tag in array of React elements)

Question: Although I see a value in the line break between brackets/braces for non-minified empty Immutable collections (see #4121) now that lib/immutable.js calls the core helper functions, and the next version of Jest will have breaking changes for snapshots, it is a good time to review whether that difference is worth the extra code logic.

Test plan

Updated: string keys in double quote marks

Added:

  • supports non-string keys
  • indentation of heterogeneous collections
  • indent option
  • maxDepth option

@pedrottimark
Copy link
Contributor Author

pedrottimark commented Aug 3, 2017

Updated 3 snapshot tests in packages/jest-matchers/src/__tests__/spy_matchers.test.js

To replace copy-paste-reuse, wrote my first iterator with help from Understanding ECMAScript 6

@pedrottimark
Copy link
Contributor Author

AppVeyor build failed: spy_matchers.test.js 3 received values don’t show the changes to plugin

Related to #4135 ?

@pedrottimark
Copy link
Contributor Author

Two questions about possible additional (or should I say, subtractional) simplifications:

  • Do test expressions require !! explicit coercion to boolean?
  • Could we merge OrderedMap into Map and OrderedSet into Set plugin as follows:
export const test = (val: any) => val && val[IS_MAP];
export const serialize = (
  val: any,
  config: Config,
  indentation: string,
  depth: number,
  refs: Refs,
  printer: Printer,
) =>
  printImmutableEntries(
    val,
    config,
    indentation,
    depth,
    refs,
    printer,
    val[IS_ORDERED] ? 'OrderedMap' : 'Map',
  );
export const test = (val: any) => val && val[IS_SET];
export const serialize = (
  val: any,
  config: Config,
  indentation: string,
  depth: number,
  refs: Refs,
  printer: Printer,
) =>
  printImmutableValues(
    val,
    config,
    indentation,
    depth,
    refs,
    printer,
    val[IS_ORDERED] ? 'OrderedSet' : 'Set',
  );

@pedrottimark
Copy link
Contributor Author

And then there was one!

I worried that it’s a wrecking change, but [ReactTestComponent, ReactElement, HTMLElement].concat(Immutable) works whether Immutable is one plugin or an array of plugins

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.

I like the change (wanted to do it myself at some point :D)


const IS_ITERABLE_SENTINEL = '@@__IMMUTABLE_ITERABLE__@@';
const IS_RECORD_SENTINEL = '@@__IMMUTABLE_RECORD__@@'; // v4 or later

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can remove these newlines between consts and between imports

printer: Printer,
): string => {
if (val[IS_SEQ_SENTINEL]) {
return '[' + getName('Seq') + ']';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we plan to support Immutable.Seq?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because Record doesn’t have a sentinel in immutable v3, serialize needs to account for all types that do. The unified test matches Seq too, which seems easiest to understand. Do you mean support it better?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yup, was thinking about adding it later, if we need it. But not as a part of this PR, let's keep it tight

@@ -20,7 +20,7 @@ exports[`lastCalledWith works with Immutable.js objects 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).not.lastCalledWith(<green>expected</><dim>)

Expected mock function to not have been last called with:
<green>[Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]</>"
<green>[Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}, Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}]</>"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is breaking, but we're more consistent 👍

' Immutable.Map {',
' "key": "value",',
' }: "immutable map",',
'}',
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think at some point we should switch such tests to template literals plus dedent

@pedrottimark
Copy link
Contributor Author

  • Renamed getName as getImmutableName
  • Moved Seq after all other types except Record (to prioritize Map and List types)
  • Deleted empty lines as requested

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.

LGTM

@pedrottimark
Copy link
Contributor Author

Thank you for investing the time to review. I don’t take it for granted.

@thymikee
Copy link
Collaborator

thymikee commented Aug 5, 2017

Still need to wait until #4135 is resolved (same thing happening in my snapshot breaking PR)

const getImmutableName = name => 'Immutable.' + name;
const SPACE = ' ';

const printImmutableEntries = (
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a lot of duplication in printImmutableEntries and printImmutableValues, but I think that extracting common logic would make it harder to understand what's going on. Not sure but I have no strong feelings about this though. Keep it as you like

@cpojer cpojer merged commit ee928fd into jestjs:master Aug 6, 2017
@pedrottimark pedrottimark deleted the immutable-serialize branch August 6, 2017 17:48
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Replace print with serialize in Immutable plugins

* Delete multiline string key to avoid conflict with 4183

* Update 3 snapshot tests

* Replace printRecordProperties with getRecordIterator

* Export 3 print functions from renamed immutable.js file

* Move comment

* Roll up immutable plugins

* Replace getName with getImmutableName

* Move Seq after all other types except Record

* Delete newlines between adjacent import and sentinel lines
@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.

4 participants