Skip to content

Commit 81efc25

Browse files
committed
Add unit test
1 parent 184ac96 commit 81efc25

File tree

6 files changed

+69
-1
lines changed

6 files changed

+69
-1
lines changed

build/webpack.base.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ module.exports = {
1111
publicPath: '/dist/',
1212
filename: 'client-bundle.js'
1313
},
14+
resolve: {
15+
extensions: ['', '.js', '.vue'],
16+
fallback: [path.join(__dirname, '../node_modules')],
17+
alias: {
18+
'src': path.resolve(__dirname, '../src'),
19+
'assets': path.resolve(__dirname, '../src/assets'),
20+
'components': path.resolve(__dirname, '../src/components')
21+
}
22+
},
1423
resolveLoader: {
1524
root: path.join(__dirname, '../node_modules'),
1625
},

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"dev": "node server",
2727
"build:client": "NODE_ENV=production webpack --config ./build/webpack.client.config.js --progress --hide-modules",
2828
"build:server": "NODE_ENV=production webpack --config ./build/webpack.server.config.js --progress --hide-modules",
29-
"build": "npm run build:client && npm run build:server"
29+
"build": "npm run build:client && npm run build:server",
30+
"unit": "karma start ./test/unit/karma.conf.js"
3031
},
3132
"dependencies": {
3233
"axios": "^0.13.1",
@@ -56,6 +57,14 @@
5657
"eslint-plugin-standard": "^2.0.0",
5758
"extract-text-webpack-plugin": "^2.0.0-beta.3",
5859
"file-loader": "^0.8.4",
60+
"karma": "^1.1.2",
61+
"karma-mocha": "^1.1.1",
62+
"karma-phantomjs-launcher": "^1.0.1",
63+
"karma-sinon-chai": "^1.2.3",
64+
"karma-sourcemap-loader": "^0.3.7",
65+
"karma-spec-reporter": "0.0.26",
66+
"karma-webpack": "^1.8.0",
67+
"phantomjs-prebuilt": "^2.1.10",
5968
"vue-loader": "^9.2.2",
6069
"webpack": "^2.1.0-beta.20",
6170
"webpack-dev-middleware": "^1.6.1",

test/unit/.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"env": {
3+
"mocha": true
4+
},
5+
"globals": {
6+
"expect": true,
7+
"sinon": true
8+
}
9+
}

test/unit/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// require all test files (files that ends with .spec.js)
2+
var testsContext = require.context('./specs', true, /\.spec$/)
3+
testsContext.keys().forEach(testsContext)

test/unit/karma.conf.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const baseConfig = require('../../build/webpack.base.config')
2+
const webpack = require('webpack')
3+
const webpackConfig = Object.assign({}, baseConfig, {
4+
devtool: '#inline-source-map',
5+
plugins: [
6+
new webpack.DefinePlugin({
7+
'process.env.NODE_ENV': '"test"'
8+
})
9+
]
10+
})
11+
12+
// no need for app entry during tests
13+
delete webpackConfig.entry
14+
15+
module.exports = function (config) {
16+
config.set({
17+
browsers: ['PhantomJS'],
18+
frameworks: ['mocha', 'sinon-chai'],
19+
reporters: ['spec'],
20+
files: ['./index.js'],
21+
preprocessors: {
22+
'./index.js': ['webpack', 'sourcemap']
23+
},
24+
webpack: webpackConfig,
25+
webpackMiddleware: {
26+
noInfo: true
27+
}
28+
})
29+
}

test/unit/specs/Home.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Vue from 'vue'
2+
import Home from 'components/Home'
3+
4+
describe('Home.vue', () => {
5+
it('should render correct home contents', () => {
6+
const vm = new Vue(Home).$mount()
7+
expect(vm.$el.textContent).to.contain('Home')
8+
})
9+
})

0 commit comments

Comments
 (0)