65

I'm getting the following error with Jest, but unclear why even after adding the testEnvironmentOptions

       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)
          at async runJest (node_modules/@jest/core/build/runJest.js:404:19)

Here is my Jest config in my package.json:

    "jest": {
        "moduleNameMapper": {
          "^@/(.*)$": "<rootDir>/$1",
          "^~/(.*)$": "<rootDir>/$1",
          "^vue$": "vue/dist/vue.common.js"
        },
        "testEnvironment": "jsdom",
        "testEnvironmentOptions": {
          "browsers": [
            "chrome",
            "firefox",
            "safari"
          ]
        },
        "moduleFileExtensions": [
          "vue",
          "js",
          "json"
        ],
        "transformIgnorePatterns": [
          "/node_modules/(?!crypto-random-string)"
        ],
        "transform": {
          "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
          ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
        },
        "collectCoverage": true,
        "collectCoverageFrom": [
          "<rootDir>/src/**/*.vue",
          "<rootDir>/src/**/*.js"
        ]
      }
1

8 Answers 8

99

Make sure you have both packages jest and jest-environment-jsdom installed in the same version. The latter is required to be installed separately from jest version 28 on. I ran into the same issue when downgrading jest to 27.x but not jest-environment-jsdom. Have a look at the version 27 to 28 upgrade guide about using JSDOM test environment.

Sign up to request clarification or add additional context in comments.

7 Comments

Boy I spent a couple of days already toiling away at this! THANK YOU for the tip of installing them separately! I owe you a beer, sir!
Is this how it is supposed to be? "devDependencies": { "jest": "^28.1.2", "jest-environment-jsdom": "^28.1.2" }
@AmirŠaran Yes, for example. Is it working?
If you have jest installed globally as I do, make sure to update it too. Otherwise, running jest from your terminal will result in the same error.
|
9

I had different versions of local jest and global jest, jest-environment-jsdom was not installed globally. This caused me the issues. Running it locally via

./node_modules/jest/bin/jest.js src/my-test.spec.ts

or updating global jest packages did the job for me. I see that this is mentioned in one of the comments above, but somehow I missed it and spent too much time on it so posting it here

3 Comments

Using pnpm workspaces, in my project I needed to add a local dependency on jest-cli that matched the local jest version.
Thank you @Sly_cardinal, that was precisely my issue also. Arggggh
Indeed, I had the same situation, but using npx jest src/my-test.spec.ts is simpler to use the project's jest :)
5

For me running

npx browserslist@latest --update-db

did the job

2 Comments

surprisingly for me too.
Same for me even though I had a message suggesting I do it which I ignored
2

I used yarn jest instead of jest to run the tests and it worked.

Comments

2

For those who still need a solution, try to check if you have a global version of the problematic library (in this case "jest-environment-node").

if you have it installed globally and the versions are different (from the running project version) you'll get errors.

I had the same issue with "jest" and with "babel-jest" when I tried to run yarn start (got a long error that started with "There might be a problem with the project dependency tree")

deleting these folders from the global path solved the problem.

the global path in Mac is usually /Users/{user}/node_modules/

good luck.

Comments

0

I removed testEnvironment: 'node' and it works perfectly

Comments

0

In my case, I had a different version of Jest globally than in my package.json. Therefore, the output of jest --version and npx jest --version were different. Using the same versions fixed the issue.

Comments

-1

If it's still not working, then please upgrade jest from 27.x to 29.x. And either remove other manual installed lib like jest-environment-node, jest-environment-jsdom (as they will come as part of jest-puppeteer) or bring these to v29.x as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.