あなたのウェブパック設定にLaravel's Elixirを使用している場合、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
カルマ設定ファイルにはウェブパック設定ファイルが必要です
webpack: webpackConfig,
唯一の問題は、Laravel's 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のは何もしていないことを示しているように見えるのエラーを取得しています。
ウェブパックの設定をテストするために、Javascriptを最小限に抑えてみましたか?単体テストを含め、今必要なものはすべて取り除き、webpack設定ファイルを再確認してください。 –
私はそう信じています。単一のvueコンポーネントをロードするtest.spec.jsファイルが1つあります。そのファイルを読み込むtests/index.jsがあります。したがって、合計2つのjsファイル、1つのvueコンポーネント、次にkarma.conf、およびkarma.webpack.conf。 –
'laravel-elixir-webpack-official'または' laravel-elixir-webpack'を使いましたか? –