Skip to content

Commit

Permalink
use karma for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan You committed Jan 13, 2014
1 parent 04249e3 commit 4129377
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 266 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ node_modules
components
explorations
test/vue.test.js
test/vue.test-cov.js
dist/vue.min.js.gz
dist/vue.min.js.gz
coverage
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ language: node_js
node_js:
- "0.10"
branches:
only:
- master
except:
- dev
before_install:
- npm install -g grunt-cli component phantomjs casperjs
before_script:
Expand Down
53 changes: 46 additions & 7 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,43 @@ module.exports = function( grunt ) {
}
},

mocha: {
test: {
src: ['test/unit/runner.html'],
karma: {
options: {
frameworks: ['mocha'],
files: [
'test/vue.test.js',
'test/unit/utils/chai.js',
'test/unit/utils/prepare.js',
'test/unit/specs/*.js'
],
singleRun: true
},
browsers: {
options: {
log: true,
run: true
browsers: ['Chrome', 'Firefox', 'Safari'],
reporters: ['progress']
}
},
phantom: {
options: {
browsers: ['PhantomJS'],
reporters: ['progress', 'coverage'],
preprocessors: {
'test/vue.test.js': ['coverage']
},
coverageReporter: {
reporters: [
{ type: 'lcov' },
{ type: 'text-summary' }
]
}
}
}
},

coveralls: {
options: {
coverage_dir: 'coverage/'
}
},

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

})

grunt.loadNpmTasks('grunt-mocha')
grunt.loadNpmTasks('grunt-karma')
grunt.loadNpmTasks('grunt-karma-coveralls')
grunt.loadNpmTasks('grunt-contrib-watch')
grunt.loadNpmTasks('grunt-contrib-jshint')

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

grunt.registerTask( 'unit', [
'instrument',
'mocha'
'karma:browsers'
])

grunt.registerTask( 'test', [
Expand All @@ -66,5 +97,13 @@ module.exports = function( grunt ) {
'build',
'test'
])

grunt.registerTask( 'travis', [
'build',
'instrument',
'karma:phantom',
'casper',
'coveralls'
])

}
22 changes: 17 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,32 @@
"url": "https://github.com/yyx990803/vue.git"
},
"scripts": {
"test": "grunt"
"test": "grunt travis"
},
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-jshint": "~0.8.0",
"grunt-mocha": "~0.4.6",
"jscoverage": "~0.3.8",
"jshint-stylish": "~0.1.4",
"semver": "~2.2.1",
"shell-task": "~0.1.1",
"map-stream": "0.0.4",
"uglify-js": "~2.4.8",
"vinyl-fs": "git://github.com/yyx990803/vinyl-fs",
"gulp-component": "~0.1.3"
"vinyl-fs": "git://github.com/wearefractal/vinyl-fs",
"gulp-component": "~0.1.4",
"grunt-karma": "~0.6.2",
"karma-script-launcher": "~0.1.0",
"karma-chrome-launcher": "~0.1.2",
"karma-firefox-launcher": "~0.1.3",
"karma-requirejs": "~0.2.1",
"karma-html2js-preprocessor": "~0.1.0",
"karma-jasmine": "~0.1.5",
"karma-coffee-preprocessor": "~0.1.2",
"karma-phantomjs-launcher": "~0.1.1",
"karma": "~0.10.9",
"karma-mocha": "~0.1.1",
"karma-coverage": "~0.1.4",
"karma-safari-launcher": "~0.1.1",
"grunt-karma-coveralls": "~2.3.0"
}
}
10 changes: 2 additions & 8 deletions tasks/instrument.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
var fs = require('vinyl-fs'),
component = require('gulp-component'),
jsc = require('jscoverage'),
map = require('map-stream')
component = require('gulp-component')

module.exports = function (grunt) {
grunt.registerTask('instrument', function () {
fs.src('./component.json')
.pipe(component.scripts({
name: 'vue.test-cov'
}))
.pipe(map(function (file, cb) {
file.contents = new Buffer(jsc.process(file.path, file.contents.toString()))
cb(null, file)
name: 'vue.test'
}))
.pipe(fs.dest('./test'))
.on('end', this.async())
Expand Down
57 changes: 12 additions & 45 deletions test/unit/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,21 @@
<head>
<title>Test</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="vendor/mocha.css">
<link rel="stylesheet" type="text/css" href="vendor/cover.css">
<link rel="stylesheet" type="text/css" href="utils/mocha.css">
</head>
<body>
<div id="mocha"></div>
<div id="test" style="display:none"></div>
<script src="vendor/mocha.js"></script>
<script src="vendor/chai.js"></script>
<script src="vendor/classList.js"></script>
<script src="vendor/mockEvent.js"></script>
<script src="vendor/cover.js"></script>
<script src="../vue.test-cov.js"></script>
<script>
mocha.setup('bdd')
var Vue = require('vue'),
assert = chai.assert

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

function mock (id, html, attrs) {
var el = document.createElement('div')
el.id = id
el.innerHTML = html
if (attrs) {
for (var attr in attrs) {
el.setAttribute(attr, attrs[attr])
}
}
document.getElementById('test').appendChild(el)
return el
}
</script>
<!-- include test utilities -->
<script src="utils/mocha.js"></script>
<script src="utils/chai.js"></script>
<script src="utils/classList.js"></script>
<script src="utils/prepare.js"></script>

<!-- specs -->
<script src="specs/utils.js"></script>
<script src="specs/binding.js"></script>
<script src="specs/directive.js"></script>
Expand All @@ -49,25 +32,9 @@
<script src="specs/transition.js"></script>
<script src="specs/batcher.js"></script>
<script src="specs/misc.js"></script>

<script>
if (navigator.userAgent.indexOf('PhantomJS') < 0) {
mocha.run(Cover.report)
} else {
// intercept test end
// and report coverage to grunt-mocha
var al = window.alert
window.alert = function (msg) {
if (msg.indexOf('mocha.end') > 0) {
var stats = Cover.getStats()[0]
console.log('\n')
console.log(' SLOC : ' + stats.sloc)
console.log(' Hits : ' + stats.hits)
console.log(' Misses : ' + stats.misses)
console.log(' Coverage : ' + stats.coverage.toFixed(2) + '%')
}
al(msg)
}
}
mocha.run()
</script>
</body>
</html>
26 changes: 14 additions & 12 deletions test/unit/specs/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,26 @@ describe('UNIT: Directive', function () {
})

describe('instantiation', function () {

var test = function () {}
directives.test = test

var obj = {
bind: function () {},
update: function () {},
unbind: function () {},
custom: function () {}
}
directives.obj = obj

it('should copy the definition as _update if the def is a function', function () {

var testDir = function () {}
directives.test = testDir

var d = Directive.parse('test', 'abc', compiler)
assert.strictEqual(d._update, test)
assert.strictEqual(d._update, testDir)
})

it('should copy methods if the def is an object', function () {

var obj = {
bind: function () {},
update: function () {},
unbind: function () {},
custom: function () {}
}
directives.obj = obj

var d = Directive.parse('obj', 'abc', compiler)
assert.strictEqual(d._update, obj.update, 'update should be copied as _update')
assert.strictEqual(d._unbind, obj.unbind, 'unbind should be copied as _unbind')
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 26 additions & 1 deletion test/unit/vendor/mockEvent.js → test/unit/utils/prepare.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function mock (id, html, attrs) {
var el = document.createElement('div')
el.id = id
el.innerHTML = html
if (attrs) {
for (var attr in attrs) {
el.setAttribute(attr, attrs[attr])
}
}
document.getElementById('test').appendChild(el)
return el
}

function mockHTMLEvent (type) {
var e = document.createEvent('HTMLEvents')
e.initEvent(type, true, true)
Expand All @@ -17,4 +30,16 @@ function mockMouseEvent (type) {
var e = document.createEvent('MouseEvent')
e.initMouseEvent(type, true, true, null, 1, 0, 0, 0, 0, false, false, false, false, 0, null)
return e
}
}

// setup

mocha.setup('bdd')
var Vue = require('vue')
var assert = chai.assert
Vue.config({silent:true})

var testDiv = document.createElement('div')
testDiv.id = 'test'
testDiv.style.display = 'none'
document.body.appendChild(testDiv)
63 changes: 0 additions & 63 deletions test/unit/vendor/cover.css

This file was deleted.

0 comments on commit 4129377

Please sign in to comment.