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

Recommend pretty-format library provided by jest #2929

Open
vhakulinen opened this issue Apr 24, 2020 · 5 comments
Open

Recommend pretty-format library provided by jest #2929

vhakulinen opened this issue Apr 24, 2020 · 5 comments

Comments

@vhakulinen
Copy link

@vhakulinen vhakulinen commented Apr 24, 2020

I was going through the testing recipes and noticed that the documentation recommends a npm module for pretty printing HTML for snapshot: https://reactjs.org/docs/testing-recipes.html#snapshot-testing.

In this example, we render a component and format the rendered HTML with the pretty package, before saving it as an inline snapshot...

The package is this: https://www.npmjs.com/package/pretty. There is another, imo stronger candidate to replace this package, which is pretty-format. Why would this be better? Its part of jest and the example code in the documentation is already using jest. Therefor it would be better to recommend a dependency from a source that the reader is already more likely to be using other dependencies from (which in this case is jest).

You can pretty print HTML with pretty-format like so:

import prettyFormat from 'pretty-format';

const pretty = (dom: HTMLElement): string => {
  return prettyFormat(
    dom,
    {
      plugins: [ prettyFormat.plugins.DOMElement ]
    }
  )
}

...

expect(
  pretty(container)
).toMatchInlineSnapshot(); /* ... gets filled automatically by jest ... */
@alexkrolick
Copy link
Collaborator

@alexkrolick alexkrolick commented Apr 24, 2020

Doesn't jest run pretty-format automatically on snapshots now?

@vhakulinen
Copy link
Author

@vhakulinen vhakulinen commented Apr 25, 2020

I'm not sure. I can test next week.

@vhakulinen
Copy link
Author

@vhakulinen vhakulinen commented Apr 28, 2020

It seems to format it when I pass an element to the expect function. It doesn't format a string (e.g. innerHTML). Maybe remove the whole pretty thing from the documentation and change the examples not to use innerHTML? Then there wouldn't be any extra dependency to recommend.

@alexkrolick
Copy link
Collaborator

@alexkrolick alexkrolick commented Apr 28, 2020

Maybe remove the whole pretty thing from the documentation and change the examples not to use innerHTML? Then there wouldn't be any extra dependency to recommend.

I think that would be fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.