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

Add a function for printing a simple DOM representation for debugging #24

Closed
jlongster opened this issue Apr 19, 2018 · 8 comments
Closed
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@jlongster
Copy link

Would you be interested in a function like this? The only annoying thing I've found with using jsdom is that it's hard to debug if you don't know what's going on or are confused. In the browser you can just look at it, but you can't with jsdom. This gives me most of what I want:

function prettyDOM(node, indent = 0) {
  let str = '';
  if (node) {
    str += ' '.repeat(indent) + (node.tagName || node.textContent) + '\n';
    for (let child of node.childNodes) {
      str += debugDOM(child, indent + 2);
    }
  }
  return str;
}

You can now do console.log(prettyDOM(node)) and get an output like this in the terminal:

screen shot 2018-04-18 at 9 36 57 pm

@jlongster
Copy link
Author

Even better, add the attached testid to the output (and maybe allow the user to pass in specific attributes they want printed?):

function debugDOM(node, indent = 0) {
  let str = '';
  if (node) {
    str += ' '.repeat(indent);
    if (node.tagName) {
      str += node.tagName + ' ' + (node.dataset['testid'] || '') + '\n';
    } else {
      str += node.textContent + '\n';
    }

    for (let child of node.childNodes) {
      str += debugDOM(child, indent + 2);
    }
  }
  return str;
}

screen shot 2018-04-18 at 9 40 42 pm

@kentcdodds
Copy link
Member

Perhaps we could expose this:

https://github.com/kentcdodds/dom-testing-library/blob/131a20bcc7cca4c0853acc49365a6c4746165471/src/queries.js#L150-L160

dab5me1v4aaehlr

What do you think?

@jlongster
Copy link
Author

Yeah, that's probably good! I like seeing a more concise form, is there a way to customize it at all? I basically don't care about most of my attributes (and there is quite a bit in my dom, which makes it harder to read). It's not a big deal though, I'd be fine just using that function.

@kentcdodds
Copy link
Member

I'd be happy to accept a PR to extract this function to its own module so we can expose it 👍

@kentcdodds kentcdodds added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Apr 19, 2018
@gnapse
Copy link
Member

gnapse commented Apr 19, 2018

@kentcdodds I can do it. Also, do you think it should publicly keep that name htmlElementToDisplay? I think I like something more along the lines of debugDOM above for the name.

@kentcdodds
Copy link
Member

Thanks @gnapse! How about prettyDOM as suggested in @jlongster's util? Perhaps we could also expose a logDOM(container) as a shortcut to console.log(prettyDOM(container)) 🤔

@gnapse gnapse mentioned this issue Apr 19, 2018
3 tasks
@Zirro
Copy link

Zirro commented Apr 19, 2018

This custom formatter and jsdom's serialize() -function may be of interest to you as well.

@kentcdodds
Copy link
Member

I'm pretty happy with the printDOM API 👍

I think I'd like to re-export it in react-testing-library :)

alexkrolick pushed a commit to alexkrolick/dom-testing-library that referenced this issue Sep 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants