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 support for pretty-printing HTMLElements #47

Closed

Conversation

quantizor
Copy link

@quantizor quantizor commented Nov 2, 2016

This builds upon and supercedes #45

Misc: also update Jest to latest

David Clark and others added 2 commits November 2, 2016 11:19
This includes support for JSDOM-created elements, as well as
real ones.
@quantizor quantizor changed the title Add support for pretty-printing HTMLElement Add support for pretty-printing HTMLElements Nov 2, 2016
@cpojer
Copy link
Contributor

cpojer commented Nov 2, 2016

I actually don't think we need to do this pretty printing. I think the right solution is to change Jest to set the max depth to ~10-20 levels or so which should solve the Jest issue.

@quantizor
Copy link
Author

@cpojer I'd rather fix the root issue than limit recursion. It's less surprising for other people using this library.

@cpojer
Copy link
Contributor

cpojer commented Nov 2, 2016

I'm not sure if this is really the root issue. Jest would still crash if there is infinite recursion on a non-html object. I also don't think HTML printing should be a default. I don't actually want to pretty-print HTML elements in Jest like this.

If we really want to do this, the right way is to build a plugin and then use instanceof checks with the objects coming from jsdom.

@quantizor
Copy link
Author

Well, I dumped 2 hours into this work. Do with it what you wish.

@jamiebuilds
Copy link
Owner

I wouldn't mind a DOM plugin for pretty-format, I think that would be quite nice actually

expect(pretty).toContain('HTMLDivElement');
expect(pretty).toContain('"className": "foo"');
expect(pretty).toContain('"id": "bar"');
expect(pretty).toContain('[HTMLSpanElement]');
Copy link
Owner

Choose a reason for hiding this comment

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

Rather than asserting the individual pieces can you assert the entire thing? Whitespace is often an issue

@@ -228,6 +287,10 @@ function printComplexValue(val, indent, prevIndent, spacing, edgeSpacing, refs,
return hitMaxDepth ? '[Map]' : printMap(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName);
} else if (toStringed === '[object Set]') {
return hitMaxDepth ? '[Set]' : printSet(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName);
} else if (isHTMLElement(val, toStringed)) {
Copy link
Owner

Choose a reason for hiding this comment

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

As mentioned in my other comment, would you mind moving this into a dom plugin that we could add to later on?

You can see some examples here: https://github.com/thejameskyle/pretty-format/tree/master/plugins

@cpojer
Copy link
Contributor

cpojer commented Nov 2, 2016

Yup, I think it's fine to add this as a plugin too!

@sophiebits
Copy link
Contributor

Node's util.inspect also displays [Circular] for any circular reference. We could do the same.

@quantizor
Copy link
Author

quantizor commented Nov 2, 2016

@spicyj It should be doing that automatically. Since all the HTMLElements end up going through printComplexValue, the ref should be stored, compared, and replaced as expected.

@quantizor
Copy link
Author

@thejameskyle I'll look into plugin-izing this later on. My code does make use of all the other functionality though, which is part of why I'm hesitant to migrate into a plugin. I'd be reimplementing all the nice handling that's part of the core lib.

@jamiebuilds
Copy link
Owner

If you need something pulled into a separate file that can be required feel free to do that like this one: https://github.com/thejameskyle/pretty-format/blob/master/printString.js

@jamiebuilds
Copy link
Owner

@spicyj pretty-format should be generating [Circular] right here: https://github.com/thejameskyle/pretty-format/blob/master/index.js#L209

I'm not sure why it's not, maybe some weird DOM magic is causing the problem?

@quantizor
Copy link
Author

I'll pick this up tomorrow after I vote

quantizor pushed a commit to quantizor/jest that referenced this pull request Nov 11, 2016
@quantizor
Copy link
Author

Going to close this for now - seems unnecessary since the library works as intended, plus some adjustments I made in #50.

@quantizor quantizor closed this Nov 11, 2016
cpojer pushed a commit to jestjs/jest that referenced this pull request Nov 14, 2016
* Set maxDepth to fix infinite recursion for HTMLElements

This change is based on the feedback from @cpojer in jamiebuilds/pretty-format#47.

* Add an integration test to verify non-crashing
nickpresta pushed a commit to nickpresta/jest that referenced this pull request Nov 15, 2016
…#2078)

* Set maxDepth to fix infinite recursion for HTMLElements

This change is based on the feedback from @cpojer in jamiebuilds/pretty-format#47.

* Add an integration test to verify non-crashing
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
…#2078)

* Set maxDepth to fix infinite recursion for HTMLElements

This change is based on the feedback from @cpojer in jamiebuilds/pretty-format#47.

* Add an integration test to verify non-crashing
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