react-testing-library: TypeError: MutationObserver is not a constructor
I have a test like
import React from "react";
import { render, screen } from "@testing-library/react";
import Countdown from "./Countdown";
describe('Countdown', () => {
    test("renders Countdown without crashing", async () => {
        const { container, getByText } = render(<Countdown timeTillDate="07 08 2020, 6:00 am" timeFormat="MM DD YYYY, h:mm a" />);
        await screen.findByText('Countdown');
        // render should show the title
        expect(getByText(/Countdown/)).toBeInTheDocument();
        expect(container).toContainElement(document.querySelector('h1'));
    });
});
This test fails with an exception
TypeError: MutationObserver is not a constructor
This same issue was raised and closed because a polyfill solved the problem. But it was also mentioned that with the latest code a polyfill was not needed. The web application that I am using was originally started with Create-React-App. Here is a snippet of my package.json to get an idea of the versions I am using:
    "@testing-library/dom": "^7.20.0",
    "@testing-library/jest-dom": "^5.11.0",
    "@testing-library/react": "^10.4.3",
    "@testing-library/user-event": "^12.0.11",
...
    "cross-env": "6.0.3",
...
    "ts-jest": "^26.1.1",
    "typescript": "3.9.6"
With these versions I shouldn’t need a polyfill right?
Thank you.
About this issue
- Original URL
 - State: closed
 - Created 4 years ago
 - Reactions: 12
 - Comments: 24 (9 by maintainers)
 
Links to this issue
Commits related to this issue
- Refs #126375 attempt to fix tests after testing upgrade: https://github.com/testing-library/react-testing-library/issues/731#issuecomment-740799553 — committed to eea/volto-slate by ichim-david 3 years ago
 - Use required jsdom environment * https://github.com/testing-library/react-testing-library/issues/731#issuecomment-667632723 * https://testing-library.com/docs/react-testing-library/setup/#jest-24-or-... — committed to vavato-be/vavato-ui by tjvc 3 years ago
 - Use required jsdom environment * https://github.com/testing-library/react-testing-library/issues/731#issuecomment-667632723 * https://testing-library.com/docs/react-testing-library/setup/#jest-24-or-... — committed to vavato-be/vavato-ui by tjvc 3 years ago
 
For other people facing the “MutationObserver is not a constructor” issue, It took me some time to figure out what’s the basic solution, so I’m posting this because it might save you some time.
go to this part in the documentation: https://testing-library.com/docs/react-testing-library/setup#jest-24-or-lower-and-defaults
react-scripts includes a dependency called jest-environment-jsdom-fourteen.
All I had to do to get rid of the MutationObserver problem, is to run the test using a jest option: --env jest-environment-jsdom-fourteen
As this one doesn’t seem to be mentioned here yet, I took my fix from an answer on this issue: https://github.com/testing-library/dom-testing-library/issues/477#issuecomment-629183432
Install
jest-environment-jsdom:Add to
package.json:Kevin try this: https://stackoverflow.com/questions/61036156/react-typescript-testing-typeerror-mutationobserver-is-not-a-constructor
Also
react-scriptsv4 is in alpha right now and it works without any extra effort FYI. It’s awesome.Its October. Fix according to developer and seems to work fine in my latest CRA project
npm install react-scripts@nextYou can also update testing library with npm-check-updates (works for yarn too )
https://github.com/raineorshine/npm-check-updates
Also it’s easier to check duplicate versions with
npm ls jsdomoryarn why jsdom. Make sure you’re using the same package manager you install the project with or the result may be incorrect.Sorry, I didn’t understand that, you’re saying it doesn’t work?
As you can see in @kentcdodds’s comment here, you can either shim it yourself our use
jest-environment-jsdom-sixteen…temp solution but I’ve added the MutationObserver package to the devDependencies and adding it to the global scope like this before running tests:
Is this the issue you were referring to? https://github.com/testing-library/react-testing-library/issues/730
For #730, the issue was that two different dependencies both specified different versions of
jsdom. It seems like that’s a possibilty here as well. If you’d like to troubleshoot this hypothesis, you can poke around yourpackage-lock.json/yarn.lockand try to figure out what versions ofjsdomare being specified.As for polyfilling… 🤔 I suspect your application/dev env is polyfilling MutationObserver appropriately, but it’s plausible that polyfilling is not applied in the same way (or at all!) to your test environment. That’s another avenue you could explore!
This issue seems pretty common, it might be worth a dedicated spot in the docs (or even a custom error message?)
Nick,
From package.json I am using version 3.4.1 and that is that latest version of react-scripts. It doesn’t look like I can upgrade react-scripts.
Kevin
On Sat, Jul 4, 2020 at 11:22 PM Nick McCurdy notifications@github.com wrote: