可在此处获得回购:https://github.com/systemnate/vuetesting
我通过运行命令rails _5.1.2_ new vuetesting --webpack=vue创建了一个rails应用程序
然后加了摩卡和chai yarn add mocha chai --save-dev
然后通过运行mkdir test/javascript创建了一个测试目录
然后我创建了一个规范文件来运行...touch app/javascript/App.spec.js
并粘贴到以下代码中:
const chai = require('chai')
const expect = chai.expect
describe('first test', () => {
it('is true', () => {
expect(true).to.equal(true)
})
})并将以下内容添加到package.json中
"scripts": {
"test": "mocha test/**/*.spec.js --recursive"
}现在,之前的纱线测试通过了。让我们尝试实现一个特定于Vue的测试。
我运行命令yarn add avoriaz mocha-webpack --save-dev
然后将package.json修改为使用mocha-webpack:
"scripts": {
"test": "mocha-webpack test/**/*.spec.js --recursive"
}yarn test仍然可以通过。
现在我想测试单个文件组件。首先,我将其添加到测试的顶部,使其如下所示:
import Vue from 'vue';
import App from '../../app/javascript/packs/App.vue';
const chai = require('chai')
const expect = chai.expect
describe('first test', () => {
it('is true', () => {
expect(true).to.equal(true)
})
})然后运行yarn test并获得以下输出
yarn test v0.27.5
$ mocha-webpack test/**/*.spec.js --recursive
ERROR in ./app/javascript/packs/App.vue
Module parse failed: /Users/natedalo/Ruby/vuetesting/app/javascript/packs/App.vue Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| <template>
| <div id="app">
| <p>{{ message }}</p>
@ ./test/javascript/App.spec.js 2:0-53
error Command failed with exit code 1.有没有想过可能出了什么问题?
发布于 2017-07-19 00:14:07
使用Unit Test Vue Components for Beginners作为指南,并从上面的链接复制webpack.config.js和.setup文件,然后将测试运行更改为在package.json中如下所示:
"test": "mocha-webpack --webpack-config test/javascript/webpack.test.config.js test/**/*.spec.js --recursive --require test/javascript/.setup"
https://stackoverflow.com/questions/45169915
复制相似问题