tutorial icon indicating copy to clipboard operation
tutorial copied to clipboard

[STEP-9] TypeError: MutationObserver is not a constructor

Open rubenslopes opened this issue 3 years ago • 5 comments

I've tried to update outdated packages but it does not solved the problem

LOG

FAIL  src/tests/app.js
  ✕ it shows a message when there are no reminders (67ms)

  ● it shows a message when there are no reminders

    TypeError: MutationObserver is not a constructor

      12 |     visit("/")
    > 13 |     await waitForElementToBeRemoved(() => screen.getByText("Loading..."))
         |           ^
      14 | 
      15 |     expect(screen.getByText("All done!")).toBeInTheDocument()
      16 |   })

      at node_modules/@testing-library/dom/dist/wait-for.js:79:18
      at waitFor (node_modules/@testing-library/dom/dist/wait-for.js:42:10)
      at node_modules/@testing-library/dom/dist/wait-for.js:135:54
      at node_modules/@testing-library/react/dist/pure.js:58:22
      at node_modules/@testing-library/react/dist/act-compat.js:60:24
      at batchedUpdates$1 (node_modules/react-dom/cjs/react-dom.development.js:21856:12)
      at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:929:14)
      at node_modules/@testing-library/react/dist/act-compat.js:59:20
      at asyncAct (node_modules/@testing-library/react/dist/act-compat.js:38:14)
      at Object.asyncWrapper (node_modules/@testing-library/react/dist/pure.js:57:35)
      at waitForWrapper (node_modules/@testing-library/dom/dist/wait-for.js:135:35)
      at waitForElementToBeRemoved (node_modules/@testing-library/dom/dist/wait-for-element-to-be-removed.js:39:31)
      at Object.<anonymous> (src/tests/app.js:13:11)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.8s, estimated 2s
Ran all test suites.

Watch Usage: Press w to show more.

rubenslopes avatar Aug 05 '20 21:08 rubenslopes

Whoops, sorry I missed this! Wasn't subscribed to the repo.

What version of node?

samselikoff avatar Aug 13 '20 18:08 samselikoff

I've run into this before, maybe six months or so ago on another React project.

Issue iirc was jest's jsdom dependency does not have MutationObserver. I think maybe I just polyfilled it as a quickfix.

More info here: https://github.com/testing-library/dom-testing-library/releases/tag/v7.0.0

ryanto avatar Aug 13 '20 19:08 ryanto

12.18.3

rubens-lopes avatar Aug 13 '20 23:08 rubens-lopes

The solution for me was this one. I updated react-scripts to the latest version and it worked.

mathiasgheno avatar Jan 25 '21 01:01 mathiasgheno

Installing the jest-environment-jsdom-sixteen package and changing the test script worked for me, see issue

"test": "craco test --env=jest-environment-jsdom-sixteen",

chazmcgrill avatar Nov 24 '21 10:11 chazmcgrill