My team is using @vue/test-utils for snapshot testing components but the snapshots are hard to parse because the HTML string output from .html() is not formatted in a readable way. If html() pretty-printed the HTML it would be a lot easier to review changes to the component-generated HTML.
html() would pretty-print the returned HTML string by default, perhaps using something like https://github.com/prettydiff/prettydiff. If necessary, a user could bypass pretty-printing by passing an option to .html(), perhaps .html(false) or .html({ prettyPrint: false }).
For snapshot tests, I recommend using the DOM element:
expect(wrapper.element).toMatchSnapshot()
Thank you for your recommendation Edd. I'm sorry that I forgot to mention that I'm mounting/testing the components in Chrome with Puppeteer using page.evaluate so the return value (which then gets its snapshot taken) has to be serializable.
I think you can just use diffable-html in your tests
I needed this too in my test! @eddyerburgh if anyone isn't actively looking into this, I'm interested to work on this, but I guess we might have to agree on the API, whether it's .html(true | false) or .html({ prettyPrint: true | false }).
@briwa @eddyerburgh I have a tentative Pull Request for this feature. Are there any preferences on .html(bool) vs .html({ prettyPrint: bool })?
@addisonssense I'd vote for .html(pretty = false) but let's hear it from the majority.
I prefer html({ prettyPrint: true | false }) in case we add extra options in the future
I have created a PR to resolve this issue. I welcome feedback :)
https://github.com/vuejs/vue-test-utils/pull/1229
Thinking about it more, I think it should pretty print by default. Vue Test Utils is still in beta so we can make breaking changes now.
Most helpful comment
For snapshot tests, I recommend using the DOM element: