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

Throw when mount/shallow rendering invalid root elements #1759

Merged
merged 1 commit into from
Aug 18, 2018

Conversation

jgzuke
Copy link
Collaborator

@jgzuke jgzuke commented Aug 16, 2018

Throw TypeError('ShallowWrapper can only wrap valid elements') or TypeError('ReactWrapper can only wrap valid elements') when ShallowWrapper or ReactWrapper are created wrapping an invalid element as root.

@jgzuke jgzuke requested a review from ljharb August 16, 2018 22:44
@jgzuke jgzuke force-pushed the jgzuke-error-on-render-invalid-roots branch from 0856e7f to b3d34b4 Compare August 16, 2018 23:13
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks, this looks great!

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

This will actually need a rebase

@@ -85,8 +85,13 @@ class ReactWrapper {
const options = makeOptions(passedOptions);

if (!root) {
const adapter = getAdapter(options);
if (!adapter.isValidElement(nodes)) {
Copy link
Member

Choose a reason for hiding this comment

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

hmm, nodes could be an array of valid elements; i'm not sure we can check it directly.

also, i think we probably want to check this on root too if that's provided?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think if we try to mount an array of elements as the root that should still throw.

root elements should always have already been through this path when they were created, ie wrap/ReactWrapper constructor only get called with null when mounting (or diving in the shallow wrapper) or this[ROOT] when traversing, which is only set here.

@jgzuke jgzuke force-pushed the jgzuke-error-on-render-invalid-roots branch 3 times, most recently from 473c67e to 4324666 Compare August 17, 2018 23:06
@jgzuke
Copy link
Collaborator Author

jgzuke commented Aug 17, 2018

The tests added failed previously with

AssertionError: expected [Function] to throw 'TypeError' but 'Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it\'s defined in, or you might have mixed up default and named imports.\n\nCheck the render method of `WrapperComponent`.' was thrown
+ expected - actual

-Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
-
-Check the render method of `WrapperComponent`.
+TypeError

at Context.<anonymous> (packages/enzyme-test-suite/test/ReactWrapper-spec.jsx:94:7)

for the mount tests and

AssertionError: expected [Function] to throw 'TypeError' but 'Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it\'s defined in, or you might have mixed up default and named imports.\n\nCheck the render method of `WrapperComponent`.' was thrown
+ expected - actual

-Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
-
-Check the render method of `WrapperComponent`.
+TypeError

at Context.<anonymous> (packages/enzyme-test-suite/test/ReactWrapper-spec.jsx:101:7)

for the shallow tests

@ljharb ljharb force-pushed the jgzuke-error-on-render-invalid-roots branch from 4324666 to 997f95a Compare August 17, 2018 23:22
@ljharb ljharb merged commit 997f95a into enzymejs:master Aug 18, 2018
ljharb added a commit that referenced this pull request Aug 25, 2018
 - [New] Add forwardRef support (#1592, @jquense)
 - [New] Add Portal support (#1760, #1761, #1772, #1774, @jgzuke)
 - [New] Add pointer events support (#1753, @ljharb)
 - [Fix] preemptively fix compat with React v16.4.3 (#1790, #1778, @gaearon, @aweary)
 - [Fix] `shallow`: prevent rerenders with PureComponents (#1786, @koba04)
 - [Fix] `shallow`: skip updates when nextState is `null` or `undefined` (#1785, @koba04)
 - [Fix] `shallow`: `setState` after `setProps` calls `componentWillReceiveProps` (#1779, @peanutenthusiast)
 - [Fix] `mount`/`shallow`: be stricter on the wrapper’s setState/setProps callback
 - [Fix] `shallow`/`mount`: improve error message when wrapping invalid elements (#1759, @jgzuke)
 - update deps
 - [Refactor] remove most uses of lodash
 - [meta] ensure a license and readme is present in all packages when published
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants