我是前端开发和Vue的新手,在尝试使用Jest和Vue-测试-库向我的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)
...
})发布于 2021-02-12 14:31:49
我在Vue项目上开玩笑,下面是我的配置:
在我的jest.config.js里我有这个
...
moduleFileExtensions: [
"js",
"json",
// tell Jest to handle `*.vue` files
"vue",
],
transform: {
// process `*.vue` files with `vue-jest` this is the most important thing to transform your VUE code!
"^.+\\.vue$": "vue-jest",
"^.+\\.js$": "babel-jest",
},
moduleNameMapper: {
"^src(.*)$": "<rootDir>/src$1",
"^lodash-es$": "lodash",
}因此,错误与在vue中使用jsx无关,因为您使用的是单一文件组件编码风格,问题是您没有转换您的.vue文件,因此需要在jest配置中添加“转换”。
我的babel配置:
module.exports = {
presets: [
"@babel/preset-env"
],
plugins: []
}我的包裹JSON:
...
"babel-jest": "26.6.3",
"@babel/preset-env": "7.12.13",
"vue-jest": "3.0.7",
...让我知道这个配置是否适合你!如果你创造一个回购,我们可以玩和调整你的配置将是更好的!
发布于 2021-03-09 05:12:38
确保安装了所有这些依赖项,以及下面的配置。
安装vue-模板编译器和vue-jest
yarn add vue-template-compiler vue-jest
# OR
npm install vue-template-compiler vue-jest安装vue-test-utils
yarn add @vue/test-utils
# OR
npm install @vue/test-utils在jest.config.js中,您会告诉大家,每个(.vue)文件都应该在vue-jest中运行。
moduleFileExtensions: ['js', 'vue'],
...
transform: {
"^.+\\.js$": "babel-jest",
"^.+\\.vue$": "vue-jest",
},如果我在某种程度上帮助了你(或没有),请与我们分享。
https://stackoverflow.com/questions/64476425
复制相似问题