如果您使用Laravel的Elixir进行webpack配置,是否可以为VueJs编写单元测试?Laravel + VueJs + Webpack + Karma =痛苦的世界
VueJs 2X有一个组件测试一个很简单的例子:Vue Guide Unit testing
<template>
<span>{{ message }}</span>
</template>
<script>
export default {
data() {
return {
message: 'hello!'
}
},
created() {
this.message = 'bye!'
}
}
</script>
然后......
// Import Vue and the component being tested
import Vue from 'vue'
import MyComponent from 'path/to/MyComponent.vue'
describe('MyComponent',() => {
it('has a created hook',() => {
expect(typeof MyComponent.created).toBe('function')
})
it ...etc
})
,在这里给出了一个因果报应的conf文件的例子:https://github.com/vuejs-templates
但是Karma配置文件需要一个webpack配置文件
webpack: webpackConfig,
唯一的问题是Laravel的Elixir正在创建webpack配置,因此无法将其包含在内。
我试着根据https://github.com/vuejs-templates/webpack的例子创建另一个webpack配置文件。
事情是这样的:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': 'vue-style-loader!css-loader!sass-loader',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
devServer: {
historyApiFallback: true,
noInfo: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
,其中包括像...
// Karma configuration
// Generated on Wed Mar 15 2017 09:47:48 GMT-0500 (CDT)
var webpackConf = require('./karma.webpack.config.js');
delete webpackConf.entry;
module.exports = function(config) {
config.set({
webpack: webpackConf, // Pass your webpack.config.js file's content
webpackMiddleware: {
noInfo: true,
stats: 'errors-only'
},
但我得到这似乎表明的WebPack没有做任何事情的错误。
ERROR in ./resources/assets/js/components/test.vue
Module parse failed: /var/www/test/resources/assets/js/components/test.vue Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| <template>
| <span >{{test}}</span>
| </template>
您是否试过用最少的测试来降低您的Javascript,以测试您的webpack配置?剥离你需要的所有东西,包括现在的单元测试,并仔细检查你的webpack配置文件。 –
我相信如此。有一个加载单个vue组件的单个test.spec.js文件。有一个测试/ index.js加载该文件。所以总共有两个js文件,一个vue组件,然后是karma.conf和karma.webpack.conf。 –
你使用过'laravel-elixir-webpack-official'或者'laravel-elixir-webpack'吗? –