-
Notifications
You must be signed in to change notification settings - Fork 474
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
Comments
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;
} |
Perhaps we could expose this: What do you think? |
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. |
I'd be happy to accept a PR to extract this function to its own module so we can expose it 👍 |
@kentcdodds I can do it. Also, do you think it should publicly keep that name |
Thanks @gnapse! How about |
This custom formatter and jsdom's |
I'm pretty happy with the I think I'd like to re-export it in react-testing-library :) |
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:
You can now do
console.log(prettyDOM(node))
and get an output like this in the terminal:The text was updated successfully, but these errors were encountered: