'JEST unit test script fails with TypeError: this._environment.runScript is not a function
I was assigned a task of setting up Jest unit testing for a Vue application. I had several attempts at accomplishing it and solved several issues on the way thanks to other questions here. However, now I'm stuck with the following error when I do npm run test:unit
("test:unit": "vue-cli-service test:unit"):
TypeError: this._environment.runScript is not a function
at Runtime._execModule (node_modules/@vue/cli-plugin-unit-jest/node_modules/jest-runtime/build/index.js:856:41)
Following is the Jest config I added to the package.json:
"jest": {
"testEnvironment": "jsdom",
"preset": "ts-jest",
"moduleNameMapper": {
"\\.(css|less|sass|scss)$": "<rootDir>/tests/mocks/styleMock.js",
"^@/(.*)$": "<rootDir>/src/$1"
},
"transform": {
"<rootDir>/src/data/.+\\.(j|t)sx?$": "ts-jest",
".*\\.(vue)$": "vue-jest",
".*\\.(js)$": "babel-jest"
},
"transformIgnorePatterns": [
"/node_modules/(?!vuetify)",
"<rootDir>/src/(?!data/.*)"
],
"testPathIgnorePatterns": [
"/node_modules/(?!vuetify)"
]
}
Originally, I had 'node' for testEnvironment. But I've gotten the this._environment.runScript is not a function error for the first time. I've read that I could use 'jsdom' instead. For that I updated babel.config.js:
module.exports = {
env: {
test: {
presets: [['env', { targets: { node: 'current' } }]],
plugins: ['@babel/plugin-transform-modules-commonjs'],
},
},
}
But this resulted in the error saying that babel-preset-env module is missing. As I understood, the module is not a stand-alone module anymore. So, I changed babel.config.js:
module.exports = {
presets: [
[
'@babel/preset-env',
{
modules: 'commonjs',
targets: {
node: 'current',
},
},
],
],
}
And now I'm back to TypeError: this._environment.runScript is not a function. Has anyone encountered a similar problem? Would appreciate your help.


Solution 1:[1]
Upgrading Jest from ^24
to ^27
fixed it for me. Now my tests are running again.
I upgraded Nodejs some days ago, maybe this caused the issue.
Solution 2:[2]
You can use a wildcard *
to match all values. Hence, you can configure a rule with id
query string and *
as value, to catch requests with id
parameter.
Then in the default route, forward to your app front page.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | fabpico |
Solution 2 | Register Sole |