【发布时间】:2021-02-05 03:02:55
【问题描述】:
我是前端开发和 Vue 的新手,在尝试使用 Jest 和 Vue-testing-library 向我的 Vue 应用程序添加“组件测试”时遇到以下错误。
FAIL tests/component-tests/vue-router.test.js
● Test suite failed to run
SyntaxError: C:\app\src\components\MyComponent.vue: Support for the experimental syntax 'jsx' isn't currently enabled (1:1):
> 1 | <template>
| ^
2 | <div class="hello">
3 | ...
4 | ...
Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
at Parser._raise (node_modules/@babel/core/node_modules/@babel/parser/src/parser/error.js:60:45)
at Parser.raiseWithData (node_modules/@babel/core/node_modules/@babel/parser/src/parser/error.js:55:17)
at Parser.expectOnePlugin (node_modules/@babel/core/node_modules/@babel/parser/src/parser/util.js:157:18)
at Parser.parseExprAtom (node_modules/@babel/core/node_modules/@babel/parser/src/parser/expression.js:1189:18)
at Parser.parseExprSubscripts (node_modules/@babel/core/node_modules/@babel/parser/src/parser/expression.js:563:23)
at Parser.parseUpdate (node_modules/@babel/core/node_modules/@babel/parser/src/parser/expression.js:543:21)
at Parser.parseMaybeUnary (node_modules/@babel/core/node_modules/@babel/parser/src/parser/expression.js:527:17)
at Parser.parseExprOps (node_modules/@babel/core/node_modules/@babel/parser/src/parser/expression.js:343:23)
at Parser.parseMaybeConditional (node_modules/@babel/core/node_modules/@babel/parser/src/parser/expression.js:308:23)
at Parser.parseMaybeAssign (node_modules/@babel/core/node_modules/@babel/parser/src/parser/expression.js:263:21)
我有另一个单元测试,它只是检查 .js 文件中的一个函数,它通过了。 谷歌搜索并最终得到以下配置
package.json
{
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test": "jest --config=./jest.config.js --coverage"
},
"dependencies": {
"core-js": "^3.6.5",
"vue": "^2.6.11",
"vue-router": "^3.2.0",
"vuetify": "^2.2.11",
"vuex": "^3.5.1"
},
"devDependencies": {
"@testing-library/vue": "^5.1.0",
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-plugin-router": "^4.5.4",
"@vue/cli-service": "~4.5.0",
"axios": "^0.18.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"jest": "^26.4.2",
"sass": "^1.19.0",
"sass-loader": "^8.0.0",
"vue-cli-plugin-axios": "0.0.4",
"vue-cli-plugin-vuetify": "~2.0.7",
"vue-template-compiler": "^2.6.11",
"vuetify-loader": "^1.3.0"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
jest.config.js
module.exports = {
moduleNameMapper: { "^@/(.*)$": "<rootDir>/src/$1" },
}
babel.config.js
module.exports = {
presets: [
'@vue/babel-preset-jsx',
'@vue/cli-plugin-babel/preset'
],
plugins: ["@babel/plugin-syntax-jsx"]
}
vue.config.js
module.exports = {
"transpileDependencies": [
"vuetify"
],
pages: {
index: {
entry: 'src/main.js',
title: 'My App'
}
}
}
tests/component-tests/vue-router.test.js
import router from '../../src/router/index.js';
import {render, fireEvent} from '@testing-library/vue'
import App from '../../src/App.vue'
test('full app rendering/navigating', async () => {
const {queryByTestId} = render(App, router.routes)
...
})
【问题讨论】:
-
这意味着 .vue 文件没有被 Vue 加载器处理,并且 Babel 无法处理它们,因为它们不是有效的 JS(X)。见github.com/vuejs/vue-jest。您是否有理由不使用 Vue CLI 中预配置的 Jest?不会有这个问题。
标签: vue.js vuejs2 jestjs babel-jest