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