@mwamitovi 这可能是我找到的最好的答案。我知道看到 Jest <24 和 babel 6 可能有点奇怪,但有时候你会在遗留代码上工作。(注意:我正在使用 vue2.js)
我通过添加上述的包和
es2015
来使其工作,像这样(jest v23.x,babel 6):
package.json:
"devDependencies": {
"@vue/test-utils": "^1.3.0",
"axios-mock-adapter": "^1.21.2",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-jest": "^23.6.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-plugin-transform-vue-jsx": "^3.7.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"jest": "^23.6.0",
"node-notifier": "^10.0.1",
"vue-jest": "^3.0.7",
"webpack": "^3.12.0",
}
jest.config.js:
module.exports = {
"verbose": true,
"testEnvironment": "jsdom",
"moduleFileExtensions": [
"js",
"json",
"vue"
],
"testMatch": [
"**/tests/*.test.(js|jsx|ts|tsx)"
],
"transform": {
".*\.(vue)$": "/node_modules/vue-jest",
".*\.(js)$": "/node_modules/babel-jest"
},
"testURL": "http://localhost/",
"transformIgnorePatterns": [
"node_modules/(?!(p-retry)/)"
]
}
and in your .babelrc
"env": {
"test": {
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
}
}],
"stage-2",
"es2015" ## this is key
],
"plugins": ["transform-vue-jsx", "transform-runtime"]
}
}
constants
是某种保留的导入。我在文件和moduleNameMapper
中将其更改为appconstants
,现在一切都正常工作了。 - dougajmcdonald