测试框架
本来想搞一下sinonJs+jasmine集成的jasmine-sinon,奈何教程太少,本着简单快速入门的原则,只搞jasmine--grunt-contrib-jasmine。而且也基本够用。
测试框架安装
cd '项目目录'
npm install grunt-contrib-jasmine --save-dev
安装完后,pacakge.json
会被改动:
{
"name": "JSCommonUtil",
"version": "1.0.0",
"engines": {
"node": ">= 0.10.0"
},
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-jshint": "~0.7.2",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-qunit": "~0.3.0",//be to delete
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-jasmine": "^0.6.3"//this has been added
}
}
配置
如果按照上一篇blog写的那样安装,你会发现Gruntfile.js
、pacakge.json
里面多了Qunit
测试框架,先将他删掉。关于banner的配置,自己看着适合着改,我自己改为:
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
'* Copyright (c) <%= grunt.template.today("yyyy") %>' +
' Licensed <%= pkg.licenses[0].type%> */\n'
接下来是加入jasmine
插件。
grunt.loadNpmTasks('grunt-contrib-jasmine');
然后便是本篇文章的重点了。先看下目录:
pub
主要用来发布整合后的js,min.js文件,其它不用多说。测试的配置思路,是现将src下的源文件合并成一个文件JSCommonUtil.js
到pub里面去,然后根据JSCommonUtil.js
,开始跑测试,测试结束后,根据JSCommonUtil.js
生成JSCommonUtil.min.js
。
本人是个格式控,所以jshint会用的比较多,以后也会搞一搞jshint的格式化输出jshint-stylish。简单的配置jshint如下,其他的关于jshint的配置,请参考grunt-contrib-jshint以及jshint官网,jshint配置比较麻烦。
jshint: {
options: {
jshintrc: true
},
beforeconcat: ['src/*.js'],
afterconcat: ['pub/*.js', '!pub/*.min.js'],
test: ['test/*.js'],
aftertest: ['pub/*.js', '!<%uglify.dist.dest%>']
}
配置concat(合并文件),由于需要一定的加载顺序,这里配置会繁琐些。
concat: {
options: {
banner: '<%= banner %>',
stripBanners: true
},
all: {
src: ['src/errorTip.js','src/collection.js,src/inject.js'],
dest: 'pub/<%= pkg.name %>.js'
}
}
====
grunt.registerTask('concats', ['jshint:beforeconcat','concat:all','jshint.afterconcat']);//请不要将task命名为concat,否则会出现循环命令
测试配置
jasmine : {
src : 'pub/*.js',
options : {
specs : 'test/*Spec.js'
}
}
=====
grunt.registerTask('test', ['jshint:test', 'jasmine']);//
剩下的就是一整套的配置了。直接列出Gruntfile.js
/*global module:false*/
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
// Metadata.
pkg: grunt.file.readJSON('package.json'),
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
'* Copyright (c) <%= grunt.template.today("yyyy") %>' +
' Licensed <%= pkg.licenses[0].type%> */\n',
//'"use strict";\n',
// Task configuration.
concat: {
options: {
banner: '<%= banner %>'
},
all: {
src: ['src/errorTip.js', 'src/collection.js', 'src/inject.js'],
dest: 'pub/<%= pkg.name %>.js'
}
},
uglify: {
options: {
banner: '<%= banner %>'
},
dist: {
src: '<%= concat.all.dest %>',
dest: 'pub/<%= pkg.name %>.min.js'
}
},
jshint: {
options: {
jshintrc: true
},
beforeconcat: ['src/*.js'],
afterconcat: ['pub/*.js', '!pub/*.min.js'],
test: ['test/*.js'],
aftertest: ['pub/*.js', '!<%uglify.dist.dest%>']
},
jasmine: {
src: 'pub/*.js',
options: {
specs: 'test/*Spec.js'
}
}
});
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-jasmine');
//grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('concats', ['jshint:beforeconcat', 'concat:all', 'jshint:afterconcat']);
grunt.registerTask('test', ['jshint:test', 'jasmine']);
grunt.registerTask('pub', ['jshint:beforeconcat', 'concat:all', 'jshint:afterconcat', 'jshint:test', 'jasmine', 'uglify:dist']);
};
以后还有其他一些简单的task,等用到再说。至于watch,先不做考虑。
运行
终于走到grunt最后一站了。但不要忘记grunt的插件还未装哦。
cd '项目目录'
npm install
接下来就是运行已经定义好的打包测试指令了。
grunt concats
grunt test
啧啧,开始享受grunt带来的快感吧。
提示:
如果你想用use strict
javascript严格模式的话,请将jshint好好的研究和配置一下。否则,满满屏幕的warning会让你明白。