Skip to content

Commit 9ea33d7

Browse files
committed
[Fix] shallow/mount: throw an explicit error when state is null/undefined
1 parent e18bb65 commit 9ea33d7

File tree

4 files changed

+8
-0
lines changed

4 files changed

+8
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ describeWithDOM('mount', () => {
161161
</div>
162162
</Foo>
163163
`.trim());
164+
expect(() => wrapper.state('key')).to.throw('ReactWrapper::state("key") requires that `state` not be `null` or `undefined`');
164165
});
165166

166167
describeIf(is('>= 16.3'), 'uses the isValidElementType from the Adapter to validate the prop type of Component', () => {

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

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ describe('shallow', () => {
153153
null
154154
</div>
155155
`.trim());
156+
expect(() => wrapper.state('key')).to.throw('ShallowWrapper::state("key") requires that `state` not be `null` or `undefined`');
156157
});
157158
});
158159

packages/enzyme/src/ReactWrapper.js

+3
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,9 @@ class ReactWrapper {
678678
}
679679
const _state = this.single('state', () => this.instance().state);
680680
if (typeof name !== 'undefined') {
681+
if (_state == null) {
682+
throw new TypeError(`ReactWrapper::state("${name}") requires that \`state\` not be \`null\` or \`undefined\``);
683+
}
681684
return _state[name];
682685
}
683686
return _state;

packages/enzyme/src/ShallowWrapper.js

+3
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,9 @@ class ShallowWrapper {
916916
}
917917
const _state = this.single('state', () => this.instance().state);
918918
if (typeof name !== 'undefined') {
919+
if (_state == null) {
920+
throw new TypeError(`ShallowWrapper::state("${name}") requires that \`state\` not be \`null\` or \`undefined\``);
921+
}
919922
return _state[name];
920923
}
921924
return _state;

0 commit comments

Comments
 (0)