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

TypeError: MutationObserver is not a constructor #357

Closed
nelsonni opened this issue Mar 4, 2021 · 0 comments · Fixed by #359
Closed

TypeError: MutationObserver is not a constructor #357

nelsonni opened this issue Mar 4, 2021 · 0 comments · Fixed by #359
Assignees
Labels
bug Bug reports or bug fixes
Milestone

Comments

@nelsonni
Copy link
Member

nelsonni commented Mar 4, 2021

Describe the bug
Running tests that use asynchronous functions (e.g. await screen.findByText(/dialog/i)) from React Testing Library (RTL) produce failing tests with the following error message:

TypeError: MutationObserver is not a constructor

Several related issues (react-testing-library#731, dom-testing-library#477 (comment), and dom-testing-library@7.0.0) provide resolution suggestions that include:

  1. Wait until the react-scripts updates to the latest version of Jest.
  2. Install jest-environment-jsdom-sixteen and then update your test script:
  ...
  "scripts": {
    ...
-   "test": "react-scripts test --env=dom"
+   "test": "react-scripts test --env=jest-environment-jsdom-sixteen"
    ...
  },
  ...
  "devDependencies": {
    ...
    "jest-environment-jsdom-sixteen": "^1.0.3",
    ...
  },
  ...
  1. Add the MutationObserver constructor to window via @sheerun/mutationobserver-shim:
npm install --save-dev @sheerun/mutationobserver-shim
# yarn add --dev @sheerun/mutationobserver-shim
// src/setupTests.js
import MutationObserver from '@sheerun/mutationobserver-shim'
window.MutationObserver = MutationObserver
  1. Manually shim the MutationObserver through a globally-scoped override import, per TypeError: MutationObserver is not a constructor testing-library/react-testing-library#731 (comment):
import MutationObserver from "mutation-observer";
global.MutationObserver = MutationObserver;
  1. Install jest-environment-jsdom:
npm i -D jest-environment-jsdom

Add to package.json:

"resolutions": {
  "jest-environment-jsdom": "^26.0.1"

Several of these solutions hinge on the existence of react-scripts as part of a Create React App project. Synectic is not a CRA app and therefore cannot upgrade react-scripts in order to obtain a dependency that uses the jest-environment-jsdom-sixteen package. Further, we use enzyme and jest-enzyme packages, in similar fashion to testing-library/dom-testing-library#477 (comment):

Maybe it would be helpful for someone...

In my case even updating to the latest jest 26.x didn't solve the problem. That was because we used to use enzyme with jest-enzyme package (we have not migrated all spec to testing-lib yet).

The problem is that the latest jest-enzyme package has dependency on jest-environement-jsdom in old version 24.9.0 🤦

Versions (please complete the following information):

  • OS: Linux, MacOS, Windows
  • Synectic Version: 1.0.0-beta (branch: development)

Additional context
N/A

@nelsonni nelsonni added the bug Bug reports or bug fixes label Mar 4, 2021
@nelsonni nelsonni self-assigned this Mar 4, 2021
@nelsonni nelsonni added this to the v1.0.0 milestone Mar 9, 2021
@nelsonni nelsonni linked a pull request Mar 9, 2021 that will close this issue
8 tasks
@nelsonni nelsonni changed the title TypeError: MutationObserver is not a constructor TypeError: MutationObserver is not a constructor Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug reports or bug fixes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant