-
-
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 alternative serialize API for pretty-format plugins #4114
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4114 +/- ##
==========================================
- Coverage 60.32% 60.31% -0.01%
==========================================
Files 195 195
Lines 6757 6756 -1
Branches 6 6
==========================================
- Hits 4076 4075 -1
Misses 2678 2678
Partials 3 3
Continue to review full report at Codecov.
|
Interpretation of performance change:
|
Added a test to specify another falsey child for which the result of |
expect(prettyFormatElementPlugin(val, options)).toEqual(formatted); | ||
expect( | ||
prettyFormatBothPlugins(renderer.create(val).toJSON(), options), | ||
).toEqual(formatted); |
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.
How about:
const formattedElementPlugin = prettyFormatElementPlugin(val, options);
const formattedBothPlugins = prettyFormatBothPlugins(renderer.create(val).toJSON(), options),
expect(formattedElementPlugin).toEqual(formattedBothPlugins);
This way we only have one expect and still test the same thing.
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.
One expect
requires both plugins to return consistent results, which is necessary but not sufficient. I’ll replace formatted
with expected
to make it clearer why 2 expect
are needed:
- Both plugins could return consistent but incorrect results.
- If one plugin is incorrect (for example,
ReactElement
for one falsey child) then a combinedexpect
has a 50% chance of implying the wrong result is expected.
Something less than ideal about assertPrintedJSX
is if a test fails, the message can’t identify which plugin :(
The test of array of elements caused me to factor out helper functions which I renamed and used in several test cases near the end:
formatElement
formatTestObject
@@ -49,13 +53,20 @@ test('supports a single element with no props', () => { | |||
); | |||
}); | |||
|
|||
test('supports a single element with number children', () => { | |||
test('supports a single element with nonzero number child', () => { |
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.
nit: non-zero
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.
Yes, thank you. And I moved the new test for empty string to make the pairs match up.
indentationNext + str.replace(NEWLINE_REGEXP, '\n' + indentationNext) | ||
); | ||
} | ||
|
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.
Any particular reason why these functions are now inlined?
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.
Yes, thank you for asking. To avoid the problem in #3962 where printPlugin
made unused closures for the callback functions, especially now that it might call either serialize
or print
property.
Oh, here is a question about const prettyFormat = require('../');
const ReactTestComponent = require('../plugins/react_test_component');
const ReactElement = require('../plugins/react_element'); Should I change it to const prettyFormat = require('../');
const {
ReactElement,
ReactTestComponent,
} = prettyFormat.plugins; to follow the pattern in
|
Forgot to say after the first round of pull requests to replace |
Feel free to adjust the plugin requires to be consistent, sure! |
We are now using Flow in the test files, please rebase. |
Yes, Flow type for tests is a super improvement. |
Good with me. Thanks again for the hard work @pedrottimark! |
* Add alternative serialize API for pretty-format plugins * Fix logic in ReactElement plugin and add test for zero number child * Add test for empty string child * Rename 2 helper functions and use in more places * Replace require paths to plugins with destructuring assignment * Add any type annotation to val arg of printPlugin * Replace overlooked occurrences of formatted with expected * Fix pretty lint error * Add comment and repeat to indent test * Add comment and repeat to indent test for react also * Fix pretty lint error in html_element.test.js
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
Fixes #3518 with breaking changes to edge cases:
Unlike core print functions for data structures, plugins have been indenting additional lines of multiline strings. It causes additional unnecessary diff line for props if a React element moves to a different level in the render tree.
In arrays of elements (from enzyme selector, or rendered fragment in React 16)
New alternative API added to
pretty-format
and implemented in this PR for 2 plugins:ReactElement
ReactTestComponent
Next step: factor out common code in these 2 plugins
Test plan
New TDD tests that failed without these improvements:
The other new tests explicitly specify existing correct behavior.
Updated snapshots because these improvements omit a redundant color reset: