51

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"
        ]
      }
panepeter
  • 3,224
  • 1
  • 29
  • 41
Soubriquet
  • 3,100
  • 10
  • 37
  • 52

5 Answers5

72

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.

Daniel
  • 958
  • 7
  • 13
  • 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! – schonarth May 08 '22 at 16:25
  • 2
    Is this how it is supposed to be? "devDependencies": { "jest": "^28.1.2", "jest-environment-jsdom": "^28.1.2" } – Amir Šaran Jul 08 '22 at 08:50
  • 1
    @AmirŠaran Yes, for example. Is it working? – Daniel Jul 11 '22 at 19:30
  • 4
    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. – Yulian Jul 28 '22 at 14:39
  • https://jestjs.io/docs/upgrading-to-jest29#jsdom-upgrade – Joe Maffei Aug 29 '22 at 14:51
  • Adding this helpful article for people still find it difficult to do it. https://bobbyhadz.com/blog/jest-typeerror-cannot-read-properties-of-undefined-reading-testenvironmentoptions – Sai kumar Feb 14 '23 at 05:39
6

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

raisedbywolves
  • 111
  • 2
  • 6
  • 2
    Using pnpm workspaces, in my project I needed to add a local dependency on `jest-cli` that matched the local `jest` version. – Sly_cardinal Jan 14 '23 at 21:30
  • Thank you @Sly_cardinal, that was precisely my issue also. Arggggh – Spock Feb 28 '23 at 09:19
  • Indeed, I had the same situation, but using `npx jest src/my-test.spec.ts` is simpler to use the project's jest :) – Pierre Mar 30 '23 at 14:23
3

For me running

npx browserslist@latest --update-db

did the job

ATilara
  • 501
  • 9
  • 14
2

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

Shubham Kundu
  • 185
  • 1
  • 10
1

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.

relbns
  • 106
  • 1
  • 4