在Vue.js中运行Jest测试

在Vue.js中运行Jest测试,vue.js,jestjs,Vue.js,Jestjs,基本上,该组件没有得到编译,因此我得到了一个意外标记,您需要使用来转换Jest Vue文件。Jest转换器是一个同步函数,它将文件和路径作为输入并输出传输的代码 我维护了一个npm包,它可以为您完成此任务-。 npm安装--保存dev vue jest 您需要将jest部分添加到package.json(或使用--config在单独的文件中)。您的配置应该如下所示: "jest": { "moduleFileExtensions": [ "js", "json",

基本上,该组件没有得到编译,因此我得到了一个
意外标记,您需要使用来转换Jest Vue文件。Jest转换器是一个同步函数,它将文件和路径作为输入并输出传输的代码

我维护了一个npm包,它可以为您完成此任务-。

npm安装--保存dev vue jest

您需要将
jest
部分添加到
package.json
(或使用--config在单独的文件中)。您的配置应该如下所示:

  "jest": {
  "moduleFileExtensions": [
    "js",
    "json",
    "vue"
  ],
  "transform": {
    "^.+\\.js$": "babel-jest",
    ".*\\.(vue)$": "vue-jest"
  }
}
这告诉jest使用jest vue作为扩展名为.vue的文件的转换


您可以在此处看到使用Vue Test Utils的有效repo-

您应该查看此页面,它解释了差异以及如何设置运行时和完整版本。谢谢,我阅读了大量指南,但在npm安装后完全跳过了此部分。安装Vue Vue Cli在配置网页包构建文件后仍然不走运,如果我有进展,我会更新
"scripts": {
    "dev": "node build/dev-server.js",
    "build": "node build/build.js",
    "test": "jest"
  },

...

"jest": {
    "unmockedModulePathPatterns": [
        "<rootDir>/node_modules/vue"
    ],
    "moduleFileExtensions": [
        "js",
        "vue"
    ],
    "scriptPreprocessor": "index.js"
}
const Vue = require("vue");
const VueTestUtils = require("vue-test-utils");

Vue.config.debug = true;
Vue.config.async = false;

Vue.use(VueTestUtils.install);

import Hello from '../src/components/Hello.vue'

const Constructor = Vue.extend(Hello)
const vm = new Constructor().$mount()

describe('initial test', () => {
  it('should be 1', () => {
    expect(1).toBe(1)
  })
})
[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
  "jest": {
  "moduleFileExtensions": [
    "js",
    "json",
    "vue"
  ],
  "transform": {
    "^.+\\.js$": "babel-jest",
    ".*\\.(vue)$": "vue-jest"
  }
}