Jest: Cannot read properties of undefined (reading 'testEnvironmentOptions')

avatar
Borislav Hadzhiev

Last updated: Mar 7, 2024
3 min

banner

# Jest: Cannot read properties of undefined (reading 'testEnvironmentOptions')

The Jest "TypeError: Cannot read properties of undefined (reading 'testEnvironmentOptions')" occurs for 2 main reasons:

  1. Having incompatible versions of jest and jest-environment-jsdom installed.
  2. Not having the jest-environment-jsdom package installed.
shell
TypeError: Cannot read properties of undefined (reading 'testEnvironmentOptions') at new JSDOMEnvironment (node_modules/jest-environment-jsdom/build/index.js:72:28) at async TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13) TypeError: Cannot read properties of undefined (reading 'html')
As of Jest 28, the jest-environment-jsdom module is no longer shipped with Jest by default and has to be installed separately.

# Install jest-environment-jsdom

Open your terminal in your project's root directory and run the following command to install jest-environment-jsdom.

shell
# ๐Ÿ‘‡๏ธ with NPM npm install jest-environment-jsdom --save-dev # ๐Ÿ‘‡๏ธ with YARN yarn add jest-environment-jsdom --dev

install jest environment jsdom

If the error persists, install jest and make sure that your jest and jest-environment-jsdom versions match.

shell
# ๐Ÿ‘‡๏ธ with NPM npm install jest --save-dev npm install jest-environment-jsdom --save-dev # ๐Ÿ‘‡๏ธ with YARN yarn add jest --dev yarn add jest-environment-jsdom --dev
If your versions of jest and jest-environment-jsdom don't match, install a matching version of the packages.

You can check your versions of jest and jest-environment-jsdom in the devDependencies section of your package.json file.

Here is an example of installing version 29.3.1 of jest and jest-environment-jsdom.

shell
# ๐Ÿ‘‡๏ธ with NPM npm install jest@29.3.1 --save-dev npm install jest-environment-jsdom@29.3.1 --save-dev # ๐Ÿ‘‡๏ธ with YARN yarn add jest@29.3.1 --dev yarn add jest-environment-jsdom@29.3.1 --dev

# Delete your node_modules and reinstall your dependencies

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your test server.

shell
# ๐Ÿ‘‡๏ธ Windows - delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json # ๐Ÿ‘‡๏ธ macOS/Linux - delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

Make sure to restart your IDE and test server if the error persists. VSCode often glitches and needs a reboot.

# Set testEnvironment to jsdom in jest.config.js

If the error persists, make sure that the testEnvironment property is set to jsdom in your package.json or jest.config.js or jest.config.ts file.

package.json
"jest": { "testEnvironment": "jsdom" }

And do the same if you use a jest.config.js file.

jest.config.js
module.exports = { "testEnvironment": "jsdom" }

Setting the testEnvironment property to jsdom allows us to test in the browser. Note that jsdom is the default testEnvironment in recent versions of jest.

If you are building a node service, set the testEnvironment property to node.

You can also set the test environment on a per-file basis with a docstring.

example.test.js
/** * @jest-environment jsdom */ test('use jsdom in this test file', () => { const element = document.createElement('div'); expect(element).not.toBeNull(); });

The documentation string at the top of the file makes it so the specified environment will be used for all tests in the file.

# Conclusion

To solve the Jest "TypeError: Cannot read properties of undefined (reading 'testEnvironmentOptions')", make sure:

  1. You have the jest-environment-jsdom package installed.
  2. Your versions of jest and jest-environment-jsdom are compatible.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev