Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing expo with enzyme: jest-environment-enzyme does not export getVmContext

I'd like to use enzyme to test my React-Native components in Expo managed project. I follow the docs for jest-expo-enzyme, but now I get:

Test environment found at "/Users/raarts/myproject/node_modules/jest-environment-enzyme/lib/index.js" does not export a "getVmContext" method, which is mandatory from Jest 27. This method is a replacement for "runScript".

My guess from here is that jest-environment-enzyme (last release 2 years ago), somehow needs to be updated for jest 27, but given jest-expo-enzyme was released 2 months ago, I figured I must be doing something wrong. Clueless on how to proceed.

Anybody has any advice?

like image 392
raarts Avatar asked Oct 17 '25 11:10

raarts


2 Answers

This is a bug in enzyme. A workaround is provided in this comment.

The solution is to force upgrade jest-environment-jsdom package. The better way to do it is via resolutions, if you are using Yarn, add this to your package.json:

  "resolutions": {
    "jest-environment-jsdom": "27.4.6"
  },

Check for the latest version of jest-environment-jsdom and change it if necessary.

You can also do this in classic NPM using the npm-force-resolutions package.

Note: you may encounter other problems after fixing this one, if you get a setImmediate error see this comment

like image 194
Grubshka Avatar answered Oct 20 '25 16:10

Grubshka


I am working on Next js project and using Jest and enzyme for testing, and for me I was able to fix this error by changing testEnvironment: 'enzyme', to testEnvironment: 'jsdom', in jest.config.js file.

like image 32
Anshu Verma Avatar answered Oct 20 '25 16:10

Anshu Verma