Vue.js &引用;支持实验语法';jsx&x27;isn';t当前已启用“;开玩笑

Vue.js &引用;支持实验语法';jsx&x27;isn';t当前已启用“;开玩笑,vue.js,vuejs2,jestjs,babel-jest,Vue.js,Vuejs2,Jestjs,Babel Jest,我不熟悉前端开发和Vue,在尝试使用Jest和将“组件测试”添加到Vue应用程序时遇到以下错误 jest.config.js module.exports = { moduleNameMapper: { "^@/(.*)$": "<rootDir>/src/$1" }, } module.exports = { presets: [ '@vue/babel-preset-jsx', '@vue/cli-plugin

我不熟悉前端开发和Vue,在尝试使用Jest和将“组件测试”添加到Vue应用程序时遇到以下错误

jest.config.js

module.exports = {
    moduleNameMapper: { "^@/(.*)$": "<rootDir>/src/$1" },
}
module.exports = {
  presets: [
    '@vue/babel-preset-jsx',
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: ["@babel/plugin-syntax-jsx"]
}
module.exports = {
  "transpileDependencies": [
    "vuetify"
  ],
  pages: {
    index: {
      entry: 'src/main.js',
      title: 'My App'
    }
  }
}
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)
    ...
})
  
yarn add @vue/test-utils
# OR
npm install @vue/test-utils
vue.config.js

module.exports = {
    moduleNameMapper: { "^@/(.*)$": "<rootDir>/src/$1" },
}
module.exports = {
  presets: [
    '@vue/babel-preset-jsx',
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: ["@babel/plugin-syntax-jsx"]
}
module.exports = {
  "transpileDependencies": [
    "vuetify"
  ],
  pages: {
    index: {
      entry: 'src/main.js',
      title: 'My App'
    }
  }
}
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)
    ...
})
  
yarn add @vue/test-utils
# OR
npm install @vue/test-utils
测试/组件测试/vue路由器.test.js

module.exports = {
    moduleNameMapper: { "^@/(.*)$": "<rootDir>/src/$1" },
}
module.exports = {
  presets: [
    '@vue/babel-preset-jsx',
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: ["@babel/plugin-syntax-jsx"]
}
module.exports = {
  "transpileDependencies": [
    "vuetify"
  ],
  pages: {
    index: {
      entry: 'src/main.js',
      title: 'My App'
    }
  }
}
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)
    ...
})
  
yarn add @vue/test-utils
# OR
npm install @vue/test-utils

我的Vue项目中有jest,以下是我的配置:

在我的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",
}
和我的包JSON:

...
"babel-jest": "26.6.3",
"@babel/preset-env": "7.12.13",
"vue-jest": "3.0.7",
...
让我知道这个配置是否适合你!
如果你创建一个回购协议,那么我们就可以随意调整你的配置,这会更好

确保安装了所有这些依赖项,并遵循以下配置

安装vue模板编译器和vue jest

yarn add vue-template-compiler vue-jest
# OR
npm install vue-template-compiler vue-jest
安装vue测试utils

module.exports = {
    moduleNameMapper: { "^@/(.*)$": "<rootDir>/src/$1" },
}
module.exports = {
  presets: [
    '@vue/babel-preset-jsx',
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: ["@babel/plugin-syntax-jsx"]
}
module.exports = {
  "transpileDependencies": [
    "vuetify"
  ],
  pages: {
    index: {
      entry: 'src/main.js',
      title: 'My App'
    }
  }
}
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)
    ...
})
  
yarn add @vue/test-utils
# OR
npm install @vue/test-utils
在jest.config.js中,您将告诉jest,每个(.vue)文件都应该通过vue jest运行

moduleFileExtensions: ['js', 'vue'],

...

transform: {
  "^.+\\.js$": "babel-jest",
  "^.+\\.vue$": "vue-jest",
},

如果我以某种方式帮助了您(或没有),请与我们分享。

这意味着.vue文件不使用vue loader处理,Babel无法处理它们,因为它们不是有效的JS(X)。看见您不使用Vue CLI中预配置的Jest有什么原因吗?它不会有这个问题。