Skip to content

Commit 4129377

Browse files
author
Evan You
committed
use karma for unit tests
1 parent 04249e3 commit 4129377

File tree

14 files changed

+121
-266
lines changed

14 files changed

+121
-266
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ node_modules
44
components
55
explorations
66
test/vue.test.js
7-
test/vue.test-cov.js
8-
dist/vue.min.js.gz
7+
dist/vue.min.js.gz
8+
coverage

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ language: node_js
22
node_js:
33
- "0.10"
44
branches:
5-
only:
6-
- master
5+
except:
6+
- dev
77
before_install:
88
- npm install -g grunt-cli component phantomjs casperjs
99
before_script:

Gruntfile.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,43 @@ module.exports = function( grunt ) {
2020
}
2121
},
2222

23-
mocha: {
24-
test: {
25-
src: ['test/unit/runner.html'],
23+
karma: {
24+
options: {
25+
frameworks: ['mocha'],
26+
files: [
27+
'test/vue.test.js',
28+
'test/unit/utils/chai.js',
29+
'test/unit/utils/prepare.js',
30+
'test/unit/specs/*.js'
31+
],
32+
singleRun: true
33+
},
34+
browsers: {
2635
options: {
27-
log: true,
28-
run: true
36+
browsers: ['Chrome', 'Firefox', 'Safari'],
37+
reporters: ['progress']
2938
}
39+
},
40+
phantom: {
41+
options: {
42+
browsers: ['PhantomJS'],
43+
reporters: ['progress', 'coverage'],
44+
preprocessors: {
45+
'test/vue.test.js': ['coverage']
46+
},
47+
coverageReporter: {
48+
reporters: [
49+
{ type: 'lcov' },
50+
{ type: 'text-summary' }
51+
]
52+
}
53+
}
54+
}
55+
},
56+
57+
coveralls: {
58+
options: {
59+
coverage_dir: 'coverage/'
3060
}
3161
},
3262

@@ -42,7 +72,8 @@ module.exports = function( grunt ) {
4272

4373
})
4474

45-
grunt.loadNpmTasks('grunt-mocha')
75+
grunt.loadNpmTasks('grunt-karma')
76+
grunt.loadNpmTasks('grunt-karma-coveralls')
4677
grunt.loadNpmTasks('grunt-contrib-watch')
4778
grunt.loadNpmTasks('grunt-contrib-jshint')
4879

@@ -53,7 +84,7 @@ module.exports = function( grunt ) {
5384

5485
grunt.registerTask( 'unit', [
5586
'instrument',
56-
'mocha'
87+
'karma:browsers'
5788
])
5889

5990
grunt.registerTask( 'test', [
@@ -66,5 +97,13 @@ module.exports = function( grunt ) {
6697
'build',
6798
'test'
6899
])
100+
101+
grunt.registerTask( 'travis', [
102+
'build',
103+
'instrument',
104+
'karma:phantom',
105+
'casper',
106+
'coveralls'
107+
])
69108

70109
}

package.json

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,32 @@
1919
"url": "https://github.com/yyx990803/vue.git"
2020
},
2121
"scripts": {
22-
"test": "grunt"
22+
"test": "grunt travis"
2323
},
2424
"devDependencies": {
2525
"grunt": "~0.4.2",
2626
"grunt-contrib-watch": "~0.5.3",
2727
"grunt-contrib-jshint": "~0.8.0",
28-
"grunt-mocha": "~0.4.6",
29-
"jscoverage": "~0.3.8",
3028
"jshint-stylish": "~0.1.4",
3129
"semver": "~2.2.1",
3230
"shell-task": "~0.1.1",
3331
"map-stream": "0.0.4",
3432
"uglify-js": "~2.4.8",
35-
"vinyl-fs": "git://github.com/yyx990803/vinyl-fs",
36-
"gulp-component": "~0.1.3"
33+
"vinyl-fs": "git://github.com/wearefractal/vinyl-fs",
34+
"gulp-component": "~0.1.4",
35+
"grunt-karma": "~0.6.2",
36+
"karma-script-launcher": "~0.1.0",
37+
"karma-chrome-launcher": "~0.1.2",
38+
"karma-firefox-launcher": "~0.1.3",
39+
"karma-requirejs": "~0.2.1",
40+
"karma-html2js-preprocessor": "~0.1.0",
41+
"karma-jasmine": "~0.1.5",
42+
"karma-coffee-preprocessor": "~0.1.2",
43+
"karma-phantomjs-launcher": "~0.1.1",
44+
"karma": "~0.10.9",
45+
"karma-mocha": "~0.1.1",
46+
"karma-coverage": "~0.1.4",
47+
"karma-safari-launcher": "~0.1.1",
48+
"grunt-karma-coveralls": "~2.3.0"
3749
}
3850
}

tasks/instrument.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
var fs = require('vinyl-fs'),
2-
component = require('gulp-component'),
3-
jsc = require('jscoverage'),
4-
map = require('map-stream')
2+
component = require('gulp-component')
53

64
module.exports = function (grunt) {
75
grunt.registerTask('instrument', function () {
86
fs.src('./component.json')
97
.pipe(component.scripts({
10-
name: 'vue.test-cov'
11-
}))
12-
.pipe(map(function (file, cb) {
13-
file.contents = new Buffer(jsc.process(file.path, file.contents.toString()))
14-
cb(null, file)
8+
name: 'vue.test'
159
}))
1610
.pipe(fs.dest('./test'))
1711
.on('end', this.async())

test/unit/runner.html

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,21 @@
33
<head>
44
<title>Test</title>
55
<meta charset="utf-8">
6-
<link rel="stylesheet" type="text/css" href="vendor/mocha.css">
7-
<link rel="stylesheet" type="text/css" href="vendor/cover.css">
6+
<link rel="stylesheet" type="text/css" href="utils/mocha.css">
87
</head>
98
<body>
109
<div id="mocha"></div>
11-
<div id="test" style="display:none"></div>
12-
<script src="vendor/mocha.js"></script>
13-
<script src="vendor/chai.js"></script>
14-
<script src="vendor/classList.js"></script>
15-
<script src="vendor/mockEvent.js"></script>
16-
<script src="vendor/cover.js"></script>
17-
<script src="../vue.test-cov.js"></script>
18-
<script>
19-
mocha.setup('bdd')
20-
var Vue = require('vue'),
21-
assert = chai.assert
2210

23-
Vue.config({silent:true})
11+
<!-- include vue -->
12+
<script src="../vue.test.js"></script>
2413

25-
function mock (id, html, attrs) {
26-
var el = document.createElement('div')
27-
el.id = id
28-
el.innerHTML = html
29-
if (attrs) {
30-
for (var attr in attrs) {
31-
el.setAttribute(attr, attrs[attr])
32-
}
33-
}
34-
document.getElementById('test').appendChild(el)
35-
return el
36-
}
37-
</script>
14+
<!-- include test utilities -->
15+
<script src="utils/mocha.js"></script>
16+
<script src="utils/chai.js"></script>
17+
<script src="utils/classList.js"></script>
18+
<script src="utils/prepare.js"></script>
19+
20+
<!-- specs -->
3821
<script src="specs/utils.js"></script>
3922
<script src="specs/binding.js"></script>
4023
<script src="specs/directive.js"></script>
@@ -49,25 +32,9 @@
4932
<script src="specs/transition.js"></script>
5033
<script src="specs/batcher.js"></script>
5134
<script src="specs/misc.js"></script>
35+
5236
<script>
53-
if (navigator.userAgent.indexOf('PhantomJS') < 0) {
54-
mocha.run(Cover.report)
55-
} else {
56-
// intercept test end
57-
// and report coverage to grunt-mocha
58-
var al = window.alert
59-
window.alert = function (msg) {
60-
if (msg.indexOf('mocha.end') > 0) {
61-
var stats = Cover.getStats()[0]
62-
console.log('\n')
63-
console.log(' SLOC : ' + stats.sloc)
64-
console.log(' Hits : ' + stats.hits)
65-
console.log(' Misses : ' + stats.misses)
66-
console.log(' Coverage : ' + stats.coverage.toFixed(2) + '%')
67-
}
68-
al(msg)
69-
}
70-
}
37+
mocha.run()
7138
</script>
7239
</body>
7340
</html>

test/unit/specs/directive.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,26 @@ describe('UNIT: Directive', function () {
113113
})
114114

115115
describe('instantiation', function () {
116-
117-
var test = function () {}
118-
directives.test = test
119-
120-
var obj = {
121-
bind: function () {},
122-
update: function () {},
123-
unbind: function () {},
124-
custom: function () {}
125-
}
126-
directives.obj = obj
127116

128117
it('should copy the definition as _update if the def is a function', function () {
118+
119+
var testDir = function () {}
120+
directives.test = testDir
121+
129122
var d = Directive.parse('test', 'abc', compiler)
130-
assert.strictEqual(d._update, test)
123+
assert.strictEqual(d._update, testDir)
131124
})
132125

133126
it('should copy methods if the def is an object', function () {
127+
128+
var obj = {
129+
bind: function () {},
130+
update: function () {},
131+
unbind: function () {},
132+
custom: function () {}
133+
}
134+
directives.obj = obj
135+
134136
var d = Directive.parse('obj', 'abc', compiler)
135137
assert.strictEqual(d._update, obj.update, 'update should be copied as _update')
136138
assert.strictEqual(d._unbind, obj.unbind, 'unbind should be copied as _unbind')
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)