Enzyme: `matchesElement()` suddenly throws an error: "cannot read property 'children' of undefined"

Created on 27 Aug 2018  ·  5Comments  ·  Source: enzymejs/enzyme

Describe the bug

Our test suite is failing because of matchesElement() calls when upgrading Enzyme from 3.3 to 3.5. Below is an example:

  ● /home/travis/build/mozilla/addons-frontend/tests/unit/amo/components/TestAddonReview.js › focuses the review text on mount
    TypeError: Cannot read property 'children' of undefined
      145 |     // This checks that reviewTextarea.focus() was called.
      146 |     expect(
    > 147 |       root.find('.AddonReview-textarea').matchesElement(document.activeElement),
          |                                          ^
      148 |     ).toEqual(true);
      149 |   });
      150 | 
      at elementToTree (node_modules/enzyme-adapter-utils/build/Utils.js:232:24)
      at elementToTree (node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:110:50)
      at ReactSixteenAdapter.elementToNode (node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:529:16)
      at ReactWrapper.<anonymous> (node_modules/enzyme/build/ReactWrapper.js:530:33)
      at ReactWrapper.single (node_modules/enzyme/build/ReactWrapper.js:1597:25)
      at ReactWrapper.matchesElement (node_modules/enzyme/build/ReactWrapper.js:528:21)
      at Object.<anonymous> (tests/unit/amo/components/TestAddonReview.js:147:42)

See:

To Reproduce

n/a

Expected behavior

The test suite should pass.

Additional context

Looks like something has changed in v3.4.0: https://github.com/airbnb/enzyme/blob/316c7c3b075930284802715b03b47d806db99b2d/CHANGELOG.md#fixes-5. It links to this commit: https://github.com/airbnb/enzyme/commit/e954e4610d1ad89ae94b8f7c7baa8835cd331662.

Most helpful comment

Hmm - .matchesElement is meant to take a react element, not a DOM element. I'm surprised this ever worked.

heh, that's good to know. I was not aware of that but it is written in the documentation 🤦‍♂️

All 5 comments

Hmm - .matchesElement is meant to take a react element, not a DOM element. I'm surprised this ever worked.

The commit that seems to be triggering the error is https://github.com/airbnb/enzyme/blob/master/packages/enzyme-adapter-utils/src/Utils.js#L197, and https://github.com/airbnb/enzyme/blob/master/packages/enzyme-adapter-utils/src/Utils.js#L193 - but document.activeElement.props would always have been undefined, and that logic would have previously existed in the react adapter directly.

Hmm - .matchesElement is meant to take a react element, not a DOM element. I'm surprised this ever worked.

heh, that's good to know. I was not aware of that but it is written in the documentation 🤦‍♂️

This seems like it's just better exposing a bug in your tests, and not actually a bug in enzyme - are you OK to close?

I think so. /cc @kumar303

Was this page helpful?
0 / 5 - 0 ratings

Related issues

potapovDim picture potapovDim  ·  3Comments

abe903 picture abe903  ·  3Comments

amcmillan01 picture amcmillan01  ·  3Comments

blainekasten picture blainekasten  ·  3Comments

ahuth picture ahuth  ·  3Comments