Skip to content

Instantly share code, notes, and snippets.

@macghriogair
Last active August 23, 2017 07:45
Show Gist options
  • Save macghriogair/30209442cb114bed63fca7752723baa9 to your computer and use it in GitHub Desktop.
Save macghriogair/30209442cb114bed63fca7752723baa9 to your computer and use it in GitHub Desktop.
Unit Test Vue + Webpack + Karma + Jasmine
{
"presets": [
[
"env",
{
"modules": false,
"targets": {
"browsers": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
}
],
"stage-2"
],
"plugins": [
"transform-runtime"
],
"env": {
"test": {
"presets": [
"env",
"stage-2"
],
"plugins": [
[
"istanbul",
{
"exclude": [
"**/tests/**/*.js"
]
}
]
]
}
}
}
// tests/index.js
import Vue from 'vue'
Vue.config.productionTip = false
// require all modules ending in "Spec" from the
// current directory and all subdirectories
const testsContext = require.context(".", true, /Spec$/);
testsContext.keys().forEach(testsContext);
// requires all components
const componentContext = require.context("../components/", true);
componentContext.keys().forEach(componentContext);
const storeContext = require.context("../store/", true);
storeContext.keys().forEach(storeContext);
const webpack = require('webpack')
const path = require('path')
const jsDir = 'resources/assets/js/'
const outputDir = path.resolve(__dirname, 'build/logs')
module.exports = function (config) {
config.set({
basePath: jsDir,
// to run in additional browsers:
// 1. install corresponding karma launcher
// http://karma-runner.github.io/0.13/config/browsers.html
// 2. add it to the `browsers` array below.
browsers: ['PhantomJS'],
frameworks: ['jasmine', 'phantomjs-shim'],
reporters: ['spec', 'coverage', 'junit'],
files: [
// https://github.com/vuejs-templates/webpack/issues/474
path.resolve(__dirname, 'node_modules/es6-promise/dist/es6-promise.auto.js'),
'tests/index.js'
],
exclude: [
],
preprocessors: {
'./tests/index.js': ['webpack', 'sourcemap']
},
webpack: {
// karma watches the test entry points
// (you don't need to specify the entry option)
devtool: '#inline-source-map',
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
},
resolve: {
extensions: ['.js'],
alias: {
components: path.resolve(__dirname, jsDir + 'components/'),
store: path.resolve(__dirname, jsDir + 'store/')
}
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"testing"'
}
})
]
},
webpackMiddleware: {
noInfo: true
},
junitReporter: {
outputFile: outputDir + '/karma-junit.xml',
suite: 'JS Unit Tests'
},
coverageReporter: {
// includeAllSources: true,
dir: path.resolve(__dirname, outputDir + '/coverage'),
reporters: [
{ type: 'lcov', subdir: 'lcov' },
{ type: 'cobertura', subdir: 'cobertura' },
{ type: 'text-summary' }
]
}
})
}
{
"scripts": {
"unit": "cross-env BABEL_ENV=test karma start karma.conf.js --single-run",
"watch-unit": "cross-env BABEL_ENV=test karma start karma.conf.js --auto-watch"
},
"devDependencies": {
"babel-plugin-istanbul": "^4.1.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"cross-env": "^5.0.5",
"jasmine-core": "^2.7.0",
"karma": "^1.7.0",
"karma-babel-preprocessor": "^6.0.1",
"karma-cli": "^1.0.1",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.1.0",
"karma-junit-reporter": "^1.2.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-phantomjs-shim": "^1.4.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "^0.0.31",
"karma-webpack": "^2.0.4",
"phantomjs-prebuilt": "^2.1.15",
"vue-loader": "^13.0.4",
"vue-style-loader": "^3.0.1",
"vue-template-compiler": "^2.4.2"
},
"dependencies": {
"vue": "^2.4.2
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment