Skip to content

Commit a7b6e78

Browse files
committedAug 28, 2018
[Fix] debug: inspect objects instead of showing them as <Component />
1 parent 2c775b4 commit a7b6e78

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
 

‎packages/enzyme-test-suite/test/Debug-spec.jsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,16 @@ describe('debug', () => {
813813
it('handles function children', () => {
814814
class Abomination extends React.Component {
815815
render() {
816-
/* eslint no-unused-vars: 0, func-names: 0 */
816+
/* eslint no-unused-vars: 0, func-names: 0, react/no-children-prop: 0 */
817817
return (
818818
<div>
819819
{function Foo() { /* hi */ }}
820820
{<span />}
821821
{arrow => arrow('function')}
822822
{[1, 2, NaN]}
823823
{function (anonymous) {}}
824+
{{ a: 'b' }}
825+
<span children={{ c: 'd' }} />
824826
</div>
825827
);
826828
}
@@ -836,6 +838,10 @@ describe('debug', () => {
836838
2
837839
NaN
838840
[function]
841+
{{ a: 'b' }}
842+
<span>
843+
{{ c: 'd' }}
844+
</span>
839845
</div>`
840846
));
841847
});

‎packages/enzyme/src/Debug.js

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import isNumber from 'is-number-object';
55
import isCallable from 'is-callable';
66
import isBoolean from 'is-boolean-object';
77
import inspect from 'object-inspect';
8+
import has from 'has';
89

910
import {
1011
propsOfNode,
@@ -67,6 +68,16 @@ function indentChildren(childrenStrs, indentLength) {
6768
: '';
6869
}
6970

71+
function isRSTNodeLike(node) {
72+
return has(node, 'nodeType')
73+
&& typeof node.nodeType === 'string'
74+
&& has(node, 'type')
75+
&& has(node, 'key')
76+
&& has(node, 'ref')
77+
&& has(node, 'instance')
78+
&& has(node, 'rendered');
79+
}
80+
7081
export function debugNode(node, indentLength = 2, options = {}) {
7182
if (typeof node === 'string' || typeof node === 'number') return escape(node);
7283
if (typeof node === 'function') {
@@ -75,6 +86,11 @@ export function debugNode(node, indentLength = 2, options = {}) {
7586
}
7687
if (!node) return '';
7788

89+
const adapter = getAdapter();
90+
if (!adapter.isValidElement(node) && !isRSTNodeLike(node)) {
91+
return `{${inspect(node)}}`;
92+
}
93+
7894
const childrenStrs = childrenOfNode(node)
7995
.map(n => debugNode(n, indentLength, options))
8096
.filter(Boolean);

0 commit comments

Comments
 (0)
Please sign in to comment.