Skip to content

Commit 997f95a

Browse files
jgzukeljharb
authored andcommitted
[Fix] shallow/mount: improve error message when wrapping invalid elements
1 parent 9ef2cc8 commit 997f95a

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,48 @@ describeWithDOM('mount', () => {
8585
expect(spy).to.have.property('callCount', 1);
8686
});
8787

88+
describe('wrapping invalid elements', () => {
89+
itIf(is('>= 16'), 'should throw when mounting Portals', () => {
90+
const portal = createPortal(
91+
<div />,
92+
{ nodeType: 1 },
93+
);
94+
95+
expect(() => mount(portal)).to.throw(
96+
Error,
97+
'ReactWrapper can only wrap valid elements',
98+
);
99+
});
100+
101+
it('should throw when mounting plain text', () => {
102+
expect(() => mount('Foo')).to.throw(
103+
Error,
104+
'ReactWrapper can only wrap valid elements',
105+
);
106+
});
107+
108+
it('should throw when mounting multiple elements', () => {
109+
expect(() => mount([<div />])).to.throw(
110+
TypeError,
111+
'ReactWrapper can only wrap valid elements',
112+
);
113+
});
114+
});
115+
116+
it('should mount built in components', () => {
117+
expect(() => mount(<div />)).not.to.throw();
118+
});
119+
120+
it('should mount composite components', () => {
121+
class Foo extends React.Component {
122+
render() {
123+
return <div />;
124+
}
125+
}
126+
127+
expect(() => mount(<Foo />)).not.to.throw();
128+
});
129+
88130
describeIf(is('>= 16.3'), 'uses the isValidElementType from the Adapter to validate the prop type of Component', () => {
89131
const Foo = () => null;
90132
const Bar = () => null;

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import './_helpers/setupAdapters';
2020
import {
2121
createClass,
2222
createContext,
23+
createPortal,
2324
createRef,
2425
Fragment,
2526
forwardRef,
@@ -77,6 +78,48 @@ describe('shallow', () => {
7778
expect(wrapper.children().type()).to.equal('div');
7879
expect(wrapper.children().props().bam).to.equal(undefined);
7980
});
81+
82+
describe('wrapping invalid elements', () => {
83+
itIf(is('>= 16'), 'should throw when shallow rendering Portals', () => {
84+
const portal = createPortal(
85+
<div />,
86+
{ nodeType: 1 },
87+
);
88+
89+
expect(() => shallow(portal)).to.throw(
90+
Error,
91+
'ShallowWrapper can only wrap valid elements',
92+
);
93+
});
94+
95+
it('should throw when shallow rendering plain text', () => {
96+
expect(() => shallow('Foo')).to.throw(
97+
Error,
98+
'ShallowWrapper can only wrap valid elements',
99+
);
100+
});
101+
102+
it('should throw when shallow rendering multiple elements', () => {
103+
expect(() => shallow([<div />])).to.throw(
104+
TypeError,
105+
'ShallowWrapper can only wrap valid elements',
106+
);
107+
});
108+
});
109+
110+
it('should shallow render built in components', () => {
111+
expect(() => shallow(<div />)).not.to.throw();
112+
});
113+
114+
it('should shallow render composite components', () => {
115+
class Foo extends React.Component {
116+
render() {
117+
return <div />;
118+
}
119+
}
120+
121+
expect(() => shallow(<Foo />)).not.to.throw();
122+
});
80123
});
81124

82125
describe('context', () => {

packages/enzyme/src/ReactWrapper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ class ReactWrapper {
8585
const options = makeOptions(passedOptions);
8686

8787
if (!root) {
88+
const adapter = getAdapter(options);
89+
if (!adapter.isValidElement(nodes)) {
90+
throw new TypeError('ReactWrapper can only wrap valid elements');
91+
}
92+
8893
privateSet(this, UNRENDERED, nodes);
89-
const renderer = getAdapter(options).createRenderer({ mode: 'mount', ...options });
94+
const renderer = adapter.createRenderer({ mode: 'mount', ...options });
9095
privateSet(this, RENDERER, renderer);
9196
renderer.render(nodes, options.context);
9297
privateSet(this, ROOT, this);

packages/enzyme/src/ShallowWrapper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ class ShallowWrapper {
170170

171171
// mounting a ShallowRender component
172172
if (!root) {
173+
if (!adapter.isValidElement(nodes)) {
174+
throw new TypeError('ShallowWrapper can only wrap valid elements');
175+
}
176+
173177
privateSet(this, ROOT, this);
174178
privateSet(this, UNRENDERED, nodes);
175179
const renderer = adapter.createRenderer({ mode: 'shallow', ...options });

0 commit comments

Comments
 (0)