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

Expose pretty-dom utility #25

Merged
merged 7 commits into from
Apr 19, 2018
Merged

Expose pretty-dom utility #25

merged 7 commits into from
Apr 19, 2018

Conversation

gnapse
Copy link
Member

@gnapse gnapse commented Apr 19, 2018

What:

This exposes the already existing internal utility function to print out a DOM in a human-friendly way.

Why:

As it was discussed in #24, this can be useful for users to print out the DOM under a certain html element, to debug more easily when writing tests.

How:

Simply moved the already existing utility to its own module.

Checklist:

  • Documentation
  • Tests
  • Code coverage

Code coverage is not full now because of the logDOM utility function. Now in hindsight I'm not sure if it's a good idea to provide it. This is because there's no eslint rule now warning the users that they left this command in use in their tests, when I think the intent is that this is only to be used while creating and changing tests, but not meant to be left in actual committed code.

If you still think we should provide it, how do you think we should resolve the code coverage issue? Should we create a new test that uses it? Wouldn't that pollute the tests output? Is there a way to ignore some lines in the coverage report?

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Looks awesome. Just a thought and we should probably document this 👍 Thanks!

printFunctionName: false,
highlight: true,
})
const maxLength = process.env.DEBUG_PRINT_LIMIT || 7000
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should accept this as an argument? I'm thinking that folks who are logging things themselves might want to control this more easily and in general probably want to see the whole thing. So perhaps we should only have this default for where it was already in use and the real default should be Infinity?

Copy link
Contributor

@antsmartian antsmartian Apr 19, 2018

Choose a reason for hiding this comment

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

Yes I agree with @kentcdodds. Currently when a test case is failing, its hiding the contents of the container and I need to increase the DEBUG_PRINT_LIMIT to see the whole dom, which is causing me to re-run the case again. Since we are going to expose this to outside world, the default value should be infinity. That should make life easier :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed it, and now the env var is only used internally when pretty printing out DOM inside thrown exceptions by the element getters. The exposed prettyDOM does not limit the output at all, and allows a second optional argument for the user to set a limit per call.

@kentcdodds
Copy link
Member

As for the coverage thing:

jest.spyOn(console, 'log').mockImplementation(() => {})
logDOM(document.createElement('div'))
expect(console.log.mock.calls).toMatchSnapshot()
console.log.mockRestore()

😄

@gnapse
Copy link
Member Author

gnapse commented Apr 19, 2018

Yes, I was aware that documentation is pending but wanted to kick off the discussion of code here. I'll change the approach to the limit. I also wanted to discuss the code coverage issue plus the potential linting issue with logDOM that I mentioned.

@gnapse
Copy link
Member Author

gnapse commented Apr 19, 2018

@kentcdodds what do you think about not providing logDOM? It's not about the coverage, but about the fact that it'd be effectively hiding a console.log from the user's eslint. A console.log that can potentially end up being committed into their vcs.

As a reference, I think I recall that enzyme's pretty print helper also did not do the console.log itself, it just gives you the output to for the user to log it out or whatever. It's also yet another API to document and maintain, when all it does it very simple.

@kentcdodds
Copy link
Member

I'd be fine with not including logDOM 👍

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

I'll go ahead and fix the one small thing and merge this. Thanks!


const {DOMElement, DOMCollection} = prettyFormat.plugins

function prettyDOM(htmlElement, maxLength = undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

Don't need the = undefined because that's functionally equivalent to what the value of maxLength would be anyway :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I know, I just like to be more explicit in the declaration that the argument is not expected, to anyone that passes by and read the code. Guess flow or typescript would be clearer for that purpose but I have yet to learn to use one of them, it's on my list ;)

@kentcdodds kentcdodds merged commit 8748c89 into testing-library:master Apr 19, 2018
@kentcdodds
Copy link
Member

🎉 This PR is included in version 1.8.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gnapse gnapse deleted the pr/pretty-dom branch April 19, 2018 21:27
@jlongster
Copy link

Wow, that was fast! Thanks all!

alexkrolick pushed a commit to alexkrolick/dom-testing-library that referenced this pull request Sep 13, 2018
* added waitForExpect with test

* added typescript and simplified version of the waitForExpect, used and exports its typings

* added initial notes about waitForExpect

* fixed styling

* minor stylistic change

* updated tests to remove the nesting

* updated readme

* fixed d .md syntax

* improved style

Closes testing-library#21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants