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

TestRenderer warns if flushThrough is passed the wrong params #12909

Merged

Conversation

bvaughn
Copy link
Contributor

@bvaughn bvaughn commented May 25, 2018

This was silently passing before, when it probably should have failed harder (in case we forgot to expect-to-equal).

The resulting test failures now will not be as pretty (wrt diff highlighting) but it's more foolproof.

@@ -576,7 +576,9 @@ describe('Profiler', () => {
</React.unstable_Profiler>,
{unstable_isAsync: true},
);
expect(renderer.unstable_flushThrough(['first'])).toEqual(['Yield:10']);
expect(renderer.unstable_flushThrough(['Yield:10'])).toEqual([
Copy link
Contributor Author

@bvaughn bvaughn May 25, 2018

Choose a reason for hiding this comment

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

This PR caught this (actual) mistake from master.

@pull-bot
Copy link

pull-bot commented May 25, 2018

Details of bundled changes.

Comparing: 76e0707...51d563e

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +0.2% +0.2% 339.29 KB 339.87 KB 73.33 KB 73.51 KB UMD_DEV
react-test-renderer.production.min.js 🔺+0.4% 🔺+0.8% 45.57 KB 45.77 KB 14.06 KB 14.18 KB UMD_PROD
react-test-renderer.development.js +0.2% +0.3% 330.12 KB 330.7 KB 70.59 KB 70.77 KB NODE_DEV
react-test-renderer.production.min.js 🔺+0.5% 🔺+0.9% 44.67 KB 44.88 KB 13.59 KB 13.72 KB NODE_PROD
ReactTestRenderer-dev.js +0.2% +0.3% 335.96 KB 336.61 KB 70.17 KB 70.37 KB FB_WWW_DEV

Generated by 🚫 dangerJS

Copy link
Collaborator

@acdlite acdlite 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. Left some nits on the error messages but in practice I don't think it matters since we probably expect folks to build their own helper on top of this so they get a nicer diff.

for (let i = 0; i < expectedValues.length; i++) {
if (yieldedValues[i] !== expectedValues[i]) {
const error = new Error(
`flushThrough expected to "${(expectedValues[
Copy link
Collaborator

Choose a reason for hiding this comment

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

Typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yargh

return [];
yieldedValues = [];
}
if (yieldedValues.length !== expectedValues.length) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't the loop below sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. I thought this error message might be more readable, but you're right- it's unnecessary.

@acdlite
Copy link
Collaborator

acdlite commented May 25, 2018

Maybe we should switch our tests to use a global flushThrough method?

global.flushThrough = (testRendererRoot, expectedValues) => {
  try {
    testRendererRoot.unstable_flushThrough(expectedValues);
  } catch (e) {
    expect(e.actualValues).toEqual(expectedValues);
  }
}

@bvaughn
Copy link
Contributor Author

bvaughn commented May 25, 2018

Yeah, maybe...

Before

screen shot 2018-05-25 at 2 24 02 pm

After

screen shot 2018-05-25 at 2 18 06 pm

In some ways, this error message is better (because of the nice diff coloring) but in other ways, it's worse- because there's no message/context. Just a couple of arrays that don't match.

We also have to remember to use it, which we may not.

Either way, this change lets us simplify all of the calls like:

expect(renderer.unstable_flushThrough([...])).toEqual([...]);

Down to:

renderer.unstable_flushThrough([...]);

Which seems like a win, since we also sometimes forgot to add those .toEqual checks.

@bvaughn
Copy link
Contributor Author

bvaughn commented May 25, 2018

Maybe I can just call jest-diff manually, like I do in the toWarnDev matcher...

global.flushThrough = (testRendererRoot, expectedValues) => {
  try {
    testRendererRoot.unstable_flushThrough(expectedValues);
  } catch (error) {
    throw new Error(error.message + '\n\n' + jestDiff(expectedValues, error.actualValues));
  }
}

Results in error messages like:
screen shot 2018-05-25 at 2 29 10 pm

But something about this still seems too awkward to do, because your code would end up looking like:

flushThrough(renderer, ['A:1']);
// Expectations...
renderer.unstable_flushAll();

@bvaughn bvaughn merged commit f35d989 into facebook:master May 25, 2018
@bvaughn bvaughn deleted the test-renderer-flush-through-validation branch May 25, 2018 21:53
NMinhNguyen referenced this pull request in enzymejs/react-shallow-renderer Jan 29, 2020
TestRenderer throws if flushThrough is passed the expected yield values that don't match actual yield values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants