2017-04-01 75 views
0

基本上,該組件是沒有得到編譯,所以我得到一個Unexpected token <錯誤,當它運行到<template>運行玩笑測試

我已經運行下面的命令:

$ npm install --save-dev jest 
$ npm install --save-dev vue-jest 
$ npm install --save-dev vue-test-utils 

我已經出了以下的package.json

"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" 
} 

我創建在根目錄下有一個簡單的測試__test__文件夾:

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.js所以它將運行使用編譯器編譯包括:

[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. 

一直在尋找了一段時間,所以任何幫助,將不勝感激

+1

您應該檢查這個頁面,它解釋的差異,如何設置和運行完整版本的 https://vuejs.org/v2/guide /installation.html –

+0

謝謝,我閱讀了一堆指南,但是在安裝完npm install vue vue-cli – gazayas

+0

後完全忽略了這一點,但配置webpack構建文件後仍然沒有運氣,如果我取得進展,我會更新 – gazayas

回答

1

您需要使用Jest transform轉換Jest Vue文件。 Jest變換器是一個同步函數,它將文件和路徑作爲輸入並輸出傳輸的代碼。

我維護一個npm包,它爲你做 - jest-vue npm install --save-dev jest-vue

你需要一個jest部分添加到您的package.json(或與--config一個單獨的文件)。你的配置應該是這個樣子:

"jest": { 
    "moduleFileExtensions": [ 
    "js", 
    "json", 
    "vue" 
    ], 
    "transform": { 
    "^.+\\.js$": "<rootDir>/node_modules/babel-jest", 
    ".*\\.(vue)$": "<rootDir>/jest-vue.js" 
    } 
} 

這告訴開玩笑用開玩笑-VUE的變換文件與.vue擴展。

您可以看到使用VUE考試utils的在這裏工作回購 - https://github.com/eddyerburgh/vue-test-utils-jest-example