Skip to content

Commit

Permalink
[Fix] debug: inspect objects instead of showing them as `<Component…
Browse files Browse the repository at this point in the history
… />`
  • Loading branch information
ljharb committed Aug 28, 2018
1 parent 2c775b4 commit a7b6e78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/enzyme-test-suite/test/Debug-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,16 @@ describe('debug', () => {
it('handles function children', () => {
class Abomination extends React.Component {
render() {
/* eslint no-unused-vars: 0, func-names: 0 */
/* eslint no-unused-vars: 0, func-names: 0, react/no-children-prop: 0 */
return (
<div>
{function Foo() { /* hi */ }}
{<span />}
{arrow => arrow('function')}
{[1, 2, NaN]}
{function (anonymous) {}}
{{ a: 'b' }}
<span children={{ c: 'd' }} />
</div>
);
}
Expand All @@ -836,6 +838,10 @@ describe('debug', () => {
2
NaN
[function]
{{ a: 'b' }}
<span>
{{ c: 'd' }}
</span>
</div>`
));
});
Expand Down
16 changes: 16 additions & 0 deletions packages/enzyme/src/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import isNumber from 'is-number-object';
import isCallable from 'is-callable';
import isBoolean from 'is-boolean-object';
import inspect from 'object-inspect';
import has from 'has';

import {
propsOfNode,
Expand Down Expand Up @@ -67,6 +68,16 @@ function indentChildren(childrenStrs, indentLength) {
: '';
}

function isRSTNodeLike(node) {
return has(node, 'nodeType')
&& typeof node.nodeType === 'string'
&& has(node, 'type')
&& has(node, 'key')
&& has(node, 'ref')
&& has(node, 'instance')
&& has(node, 'rendered');
}

export function debugNode(node, indentLength = 2, options = {}) {
if (typeof node === 'string' || typeof node === 'number') return escape(node);
if (typeof node === 'function') {
Expand All @@ -75,6 +86,11 @@ export function debugNode(node, indentLength = 2, options = {}) {
}
if (!node) return '';

const adapter = getAdapter();
if (!adapter.isValidElement(node) && !isRSTNodeLike(node)) {
return `{${inspect(node)}}`;
}

const childrenStrs = childrenOfNode(node)
.map(n => debugNode(n, indentLength, options))
.filter(Boolean);
Expand Down

0 comments on commit a7b6e78

Please sign in to comment.