Skip to content

Commit c1ae57d

Browse files
committed
Add unit tests with karma
1 parent 77c04fd commit c1ae57d

File tree

8 files changed

+106
-22
lines changed

8 files changed

+106
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules/
33
.vagrant
44
npm-debug.*
55
*.sublime-*
6+
test/unit/coverage/
File renamed without changes.

memo.txt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
=== NPM Permissions : https://docs.npmjs.com/getting-started/fixing-npm-permissions ===
22

33
1.
4-
sudo nano /etc/apache2/sites-available/000_default.conf (remove /public on DocumentRoot)
4+
sudo nano /etc/apache2/sites-available/000-default.conf
5+
(remove /public on DocumentRoot)
56

67

78
2.
@@ -13,16 +14,7 @@ mkdir ~/.npm-global && npm config set prefix '~/.npm-global' && export PATH=~/.n
1314

1415

1516
4.
16-
mkdir ~/.npm-global/lib
17-
18-
4.1.
19-
mkdir ~/.npm-global/lib/node_modules
20-
21-
4.2.
22-
mkdir ~/.npm-global/bin
23-
24-
4.3.
25-
mkdir ~/.npm-global/share
17+
mkdir ~/.npm-global/lib && mkdir ~/.npm-global/lib/node_modules && mkdir ~/.npm-global/bin && mkdir ~/.npm-global/share
2618

2719

2820
5.
@@ -40,3 +32,17 @@ sudo npm install cross-env webpack webpack-dev-server --save
4032
8.
4133
npm run dev
4234

35+
36+
37+
=== KARMA ===
38+
39+
1.
40+
npm i -g karma-cli --save
41+
42+
2.
43+
export PHANTOMJS_BIN=/home/vagrant/.npm-global/bin/phantomjs
44+
45+
46+
47+
3.
48+
npm run test

package.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,40 @@
55
"author": "Mickael SURREL",
66
"scripts": {
77
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
8-
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
8+
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules",
9+
"test": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run"
910
},
1011
"dependencies": {
1112
"ChuckCSS": "^3",
1213
"cross-env": "^3.2.4",
14+
"karma-cli": "^1.0.1",
1315
"lodash": "^4.17.4",
16+
"phantomjs": "^2.1.7",
1417
"vue": "^2.2",
15-
"webpack": "^2.2.1",
18+
"webpack": "^2.3.1",
1619
"webpack-dev-server": "^2.4.2"
1720
},
1821
"devDependencies": {
1922
"autoprefixer": "^6.7.6",
2023
"axios": "^0.15.3",
2124
"babel-core": "^6.0.0",
2225
"babel-loader": "^6.0.0",
26+
"babel-polyfill": "^6.23.0",
2327
"babel-preset-es2015": "^6.0.0",
2428
"cross-env": "^3.2.3",
2529
"css-loader": "^0.25.0",
2630
"file-loader": "^0.9.0",
2731
"highcharts": "^5",
32+
"jasmine-core": "^2.5.2",
2833
"json-loader": "^0.5.4",
34+
"karma": "^1.5.0",
35+
"karma-babel-preprocessor": "^6.0.1",
36+
"karma-coverage": "^1.1.1",
37+
"karma-jasmine": "^1.1.0",
38+
"karma-phantomjs-launcher": "^1.0.4",
39+
"karma-sourcemap-loader": "^0.3.7",
40+
"karma-spec-reporter": "0.0.30",
41+
"karma-webpack": "^2.0.3",
2942
"less": "^2.7.2",
3043
"less-loader": "^2.2.3",
3144
"moment": "^2.17.1",
@@ -38,7 +51,6 @@
3851
"vue-loader": "^10.0.0",
3952
"vue-router": "^2.2.1",
4053
"vue-template-compiler": "^2.2",
41-
"vuelidate": "^0.4",
42-
"vuex": "^2.2.1"
54+
"vuelidate": "^0.4"
4355
}
4456
}

src/js/views/dashboard/dashboard.vue

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@
2626
<li>Statistiques</li>
2727
<li>Logs</li>
2828
<li>Notify</li>
29-
<li>Tests with :
30-
<ul>
31-
<li><a href="https://github.com/avajs/ava">Ava.js</a></li>
32-
<li><a href="https://karma-runner.github.io/1.0/index.html">Karma</a></li>
33-
<li><a href="https://jasmine.github.io/">Jasmine</a></li>
34-
</ul>
35-
</li>
3629
</ul>
3730
</div>
3831
</div>

test/unit/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Vue from 'vue'
2+
Vue.config.productionTip = false
3+
4+
// Polyfill fn.bind() for PhantomJS
5+
/* eslint-disable no-extend-native */
6+
Function.prototype.bind = require('function-bind')
7+
8+
// require all test files (files that ends with .spec.js)
9+
const testsContext = require.context('./specs', true, /\.spec$/)
10+
testsContext.keys().forEach(testsContext)
11+
12+
// require all src files except main.js for coverage.
13+
// you can also change this to match only the subset of files that
14+
// you want coverage for.
15+
const srcContext = require.context('../../src/js', true, /^\.\/(?!main(\.js)?$)/)
16+
srcContext.keys().forEach(srcContext)

test/unit/karma.conf.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Karma configuration
2+
3+
var webpackConf = require('../../webpack.config.js');
4+
delete webpackConf.entry
5+
6+
module.exports = function (config) {
7+
config.set({
8+
9+
browsers: ['PhantomJS'],
10+
11+
frameworks: ['jasmine'],
12+
13+
reporters: ['spec', 'coverage'],
14+
15+
files: [
16+
'../../node_modules/babel-polyfill/dist/polyfill.js',
17+
'./index.js'
18+
],
19+
20+
preprocessors: {
21+
'./index.js': ['webpack', 'sourcemap']
22+
},
23+
24+
webpack: webpackConf,
25+
26+
webpackMiddleware: {
27+
noInfo: true
28+
},
29+
30+
coverageReporter: {
31+
dir: './coverage',
32+
reporters: [
33+
{
34+
type: 'lcov',
35+
subdir: '.'
36+
},
37+
{
38+
type: 'text-summary'
39+
}
40+
]
41+
}
42+
})
43+
}

test/unit/specs/dashboard.spec.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 dashboardTest from '../../../src/js/views/dashboard/dashboard.vue'
3+
4+
5+
// Jasmine 2.0 tests
6+
describe('dashboardTest', () => {
7+
8+
it('correctly sets loading status when mounted', () => {
9+
const Ctor = Vue.extend(dashboardTest)
10+
const vm = new Ctor().$mount()
11+
expect(vm.loading).toBe(false)
12+
})
13+
})

0 commit comments

Comments
 (0)