unit-testingjestjs

Jest: testEnvironmentOptions cannot be read


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"
        ]
      }

Solution

  • 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.