Mutation observer is not a constructor (Jest)

3.9k views Asked by At

I am trying to use Jest with a new CRA project, but I am facing a problem when trying to use the findByText function. I installed:

yarn add -D jest-environment-jsdom-sixteen

And run my tests with react-scripts test --env=jest-environment-jsdom-sixteen. It works fine if I do this solution that I encountered while reading these issues here:

However I am also using the Jest plugin from VS code and tests do not pass with it. I have tried to do this in my jest.config.js as suggested in the docs:

module.exports = {
  testEnvironment: 'jest-environment-jsdom-sixteen',
};

But it still does not work.

Thanks in advance.

1

There are 1 answers

1
Mojo On

Testing MutationObserver with Jest: Try this, it worked for me, on a similar error:

  1. npm i -D mutationobserver-shim
  2. import "mutationobserver-shim"; in setupTests.js