gpt4 book ai didi

javascript - 使用 Jest 测试 Vue 失败,Jest 遇到意外标记,SyntaxError : Unexpected token import

转载 作者:搜寻专家 更新时间:2023-10-30 22:54:57 24 4
gpt4 key购买 nike

我用 jest 测试 Vue 项目,我得到错误的 bug 说:

Jest encountered an unexpected token and SyntaxError: Unexpected token import

这是我的 jest.conf.js,我用 babel-jest 和 vue jest 设置了“transorm”选项,但我仍然收到错误消息说无法转换 es6 语法

const path = require('path')

module.exports = {
rootDir: path.resolve(__dirname, '../../'),
moduleFileExtensions: [
'js',
'json',
'vue'
],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
transform: {
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
'.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
},
testPathIgnorePatterns: [
'<rootDir>/test/e2e'
],
snapshotSerializers: ['<rootDir>/node_modules/jest-serializer-vue'],
setupFiles: ['<rootDir>/test/unit/setup'],
coverageDirectory: '<rootDir>/test/unit/coverage',
collectCoverageFrom: [
'src/**/*.{js,vue}',
'!src/main.js',
'!src/router/index.js',
'!**/node_modules/**'
]
}

这是 .babelrc 配置:

{
"presets": [
[
"env",
{
"modules": false,
"targets": {
"browsers": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
}
],
"stage-2"
],
"plugins": [
"transform-vue-jsx",
"transform-runtime",
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "~node_modules/modeling-theme-element/lib"
}
]
],
"env": {
"test": {
"presets": [
"env",
"jest",
"stage-2"
],
"plugins": [
[
"dynamic-import-node",
{
"root": [
"./src"
],
"alias": {
"@": "./src"
}
}
]
]
}
}
}

最佳答案

我遇到了类似的问题,“babel-plugin-transform-es2015-modules-commonjs”正在为我工​​作


npm i -D babel-plugin-transform-es2015-modules-commonjs

.babelrc 配置
“插件”:[“transform-es2015-modules-commonjs”]

关于javascript - 使用 Jest 测试 Vue 失败,Jest 遇到意外标记,SyntaxError : Unexpected token import,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52107923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com