react-testing-library icon indicating copy to clipboard operation
react-testing-library copied to clipboard

TypeError: MutationObserver is not a constructor in version 10.0.0 and above

Open bahdcoder opened this issue 6 years ago • 6 comments

  • @testing-library/react version: 10.0.0 and above
  • Testing Framework and version: Jest, tested with ^26.1.0, ^24.9.0
  • DOM Environment: Jsdom. Tested with latest Jest.

Relevant code or config:

expect((await findAllByTestId('ProductTile'))).toHaveLength(defaultProducts.length)

What you did:

Tried to use the findAllByTestId method with @testinglibrary/react v10.0.0 and all versions above that (incrementally until v10.4.3

What happened:

Threw the error TypeError: MutationObserver is not a constructor for all of these versions.

I then downgraded to v9.5.0 and the test works without that error.

Reproduction:

  • Install any version after v10.0.0 of this package.
  • Try using the findAllByTestId query method.

Problem description:

Suggested solution:

Digging in to check what changed in v10.0.0 .

bahdcoder avatar Jul 03 '20 18:07 bahdcoder

I read the changelogs for dom testing library. The notes imply that jest uses the latest version of Jsdom, and if you upgrade to the latest version of Jest, all should work fine.

But that is not the case.

The latest version of Jest still installs Jsdom version 11 🤔 . I don't know why. It's really strange. Already sounds like a Jest problem to me.

This link shows that Jest should install v16 of jsdom, but the node_modules has v11.

bahdcoder avatar Jul 03 '20 19:07 bahdcoder

Looks like MutationObserver was implemented as of jsdom 13.2.0. https://github.com/jsdom/jsdom/releases/tag/13.2.0

Here's the dependency spec for jest (and jest-environment-jsdom) for 24.9.0 https://github.com/facebook/jest/blob/v24.9.0/packages/jest-environment-jsdom/package.json

I just had this problem! I can verify that upgrading jest (and jsdom, transitively) and @testing-library/react to their latest versions (at the time) resolved the issue

Is it possible you have another dependency in your node_modules that's requesting the older version of jsdom (I did, at the time! That was my issue; I think it was something from the enzyme ecosystem)

tjefferson08 avatar Jul 03 '20 19:07 tjefferson08

Ah right ! Thanks a lot for sharing @tjefferson08 . I was using some Enzyme things too. Removed all the enzyme things and all is well again. Been debugging for 6-ish hours and wanted to be super sure before opening an issue.

Thanks for helping out !

Wondering if its possible to force the jsdom version ? I am creating a teaching workshop and I was hoping to show enzyme and testing library differences in the same codebase.

bahdcoder avatar Jul 03 '20 19:07 bahdcoder

Awesome @bahdcoder! Right place, right time 🙌 I'm glad it worked for you.

I think there are ways to mandate a particular version of subdependencies be installed with both npm and yarn, that's probably something you could hunt down with some good googling 👍

As to your desire to demo both, hm... perhaps you could do something hacky like maintaining two different package.json files and swapping back and forth?

tjefferson08 avatar Jul 03 '20 19:07 tjefferson08

Sounds like a great idea to try out. Thanks for your time and support. Closing, but I hope this thread helps some folks like me in future.

bahdcoder avatar Jul 03 '20 19:07 bahdcoder

Wondering if its possible to force the jsdom version ? I am creating a teaching workshop and I was hoping to show enzyme and testing library differences in the same codebase.

If you are using Yarn, you should be able to force dependency resolution using resolutions in your package.json (see docs). NPM doesn't have this built in, but you should be able to use npm-force-resolutions.

kylecombes avatar Nov 02 '20 16:11 kylecombes