Skip to content

Commit 2791865

Browse files
committed
Create simple unit test with mocha, chai and karma
1 parent b1ab1a4 commit 2791865

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

app/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ var simpleFiles = {
1010
'_eslintrc.json': 'src/.eslintrc.json',
1111
'_route.js': 'src/route.js',
1212
'_Hello.vue': 'src/components/Hello.vue',
13-
'_logo.png': 'src/assets/logo.png'
13+
'_logo.png': 'src/assets/logo.png',
14+
'_test.index.js': 'test/unit/index.js',
15+
'_test.spec.Hello.js': 'test/unit/specs/Hello.spec.js',
16+
'_test.eslintrc.json': 'test/unit/.eslintrc.json',
17+
'_test.karma.conf.js': 'karma.conf.js'
1418
};
1519
var tplFiles = {
1620
'_index.html': 'index.html',

app/templates/_package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "<%= appname %> project",
55
"scripts": {
66
"dev": "webpack-dev-server --inline --hot --quiet",
7-
"build": "rm -rf build/ && NODE_ENV=production webpack --progress --hide-modules"
7+
"build": "rm -rf build/ && NODE_ENV=production webpack --progress --hide-modules",
8+
"test": "karma start --single-run"
89
},
910
"dependencies": {<% if (extraConfig.isUseVueRouter) { %>
1011
"vue-router": "^0.7.13",<% } %>
@@ -16,6 +17,7 @@
1617
"babel-plugin-transform-runtime": "^6.7.5",
1718
"babel-preset-es2015": "^6.6.0",
1819
"babel-runtime": "^5.8.0",
20+
"chai": "^3.5.0",
1921
"css-loader": "^0.23.1",
2022
"eslint": "^2.8.0",
2123
"eslint-config-airbnb-base": "^1.0.4",
@@ -25,8 +27,15 @@
2527
"extract-text-webpack-plugin": "^1.0.1",
2628
"file-loader": "^0.8.5",
2729
"html-webpack-plugin": "^2.16.0",
30+
"karma": "^0.13.22",
31+
"karma-chai": "^0.1.0",
32+
"karma-mocha": "^1.0.1",
33+
"karma-phantomjs-launcher": "^1.0.0",
34+
"karma-webpack": "^1.7.0",
2835
"less": "^2.6.1",
2936
"less-loader": "^2.2.3",
37+
"mocha": "^2.4.5",
38+
"phantomjs-prebuilt": "^2.1.7",
3039
"style-loader": "^0.13.1",
3140
"url-loader": "^0.5.7",
3241
"vue-hot-reload-api": "^1.3.2",

app/templates/_test.eslintrc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "airbnb-base",
3+
"env": {
4+
"mocha": true
5+
},
6+
"globals": {
7+
"expect": true
8+
},
9+
"settings": {
10+
"import/resolver": {
11+
"node": {
12+
"extensions": [".js", ".json", ".vue"]
13+
}
14+
}
15+
}
16+
}

app/templates/_test.index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const testsContext = require.context('./specs', true, /\.spec$/);
2+
testsContext.keys().forEach(testsContext);

app/templates/_test.karma.conf.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var webpackConfig = require('./webpack.config');
2+
delete webpackConfig.entry;
3+
4+
// Karma configuration
5+
module.exports = function(config) {
6+
config.set({
7+
// frameworks to use
8+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
9+
frameworks: ['mocha', 'chai'],
10+
// list of files / patterns to load in the browser
11+
files: [
12+
'./test/unit/index.js'
13+
],
14+
// preprocess matching files before serving them to the browser
15+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
16+
preprocessors: {
17+
'./test/unit/index.js': 'webpack'
18+
},
19+
// start these browsers
20+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
21+
browsers: ['PhantomJS'],
22+
// test results reporter to use
23+
// possible values: 'dots', 'progress'
24+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
25+
// reporters: ['progress'],
26+
webpack: webpackConfig,
27+
webpackMiddleware: {
28+
noInfo: true
29+
}
30+
});
31+
}

app/templates/_test.spec.Hello.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Vue from 'vue';
2+
import Hello from '../../../src/components/Hello';
3+
4+
describe('Hello.vue', () => {
5+
it('should render correct contents', () => {
6+
const vm = new Vue({
7+
template: '<div><hello></hello></div>',
8+
components: { Hello },
9+
}).$mount();
10+
11+
expect(vm.$el.querySelector('.hello h1').textContent).to.contain('Hello World!');
12+
});
13+
});

0 commit comments

Comments
 (0)