Jasmine 是一個含有豐富的斷言庫的測試框架。目前我用的最新的版本是:2.6node
安裝npm
npm install -g jasmine //這裏採用全局安裝,好處是直接cmd就能用,也能夠採用本地安裝
初始化配置文件json
jasmine init
生成的配置文件以下jasmine.json
:app
{ "spec_dir": "spec", //spec 所在目錄 "spec_files": [ "**/*[sS]pec.js" //測試文件,相對於spec_dir ], "helpers": [ "helpers/**/*.js" //測試前輔助文件,相對於spec_dir ], "stopSpecOnExpectationFailure": false, // "random": false }
運行測試框架
//直接根據配置文件運行 jasmine //執行測試某個文件 jasmine appSpec.js
var Jasmine = require('jasmine'); var jasmine = new Jasmine();
加載配置文件dom
//方式1 jasmine.loadConfigFile('spec/support/jasmine.json'); //方式2 jasmine.loadConfig({ spec_dir: 'spec', spec_files: [ 'appSpec.js', 'requests/**/*[sS]pec.js', 'utils/**/*[sS]pec.js' ], helpers: [ 'helpers/**/*.js' ] });
自定義測試完成事件測試
jasmine.onComplete(function(passed) { if(passed) { console.log('All specs have passed'); } else { console.log('At least one spec has failed'); } });
自定義測試報告ui
jasmine.configureDefaultReporter({ timer: new this.jasmine.Timer(), print: function() { process.stdout.write(util.format.apply(this, arguments)); }, showColors: true, jasmineCorePath: this.jasmineCorePath }); var CustomReporter = require('./myCustomReporter'); var customReporter = new CustomReporter(); jasmine.addReporter(customReporter);
執行測試this
jasmine.execute(); jasmine.execute(['fooSpec.js'], 'a spec name');
簡單完整的測試案例命令行
var Jasmine = require('jasmine'); var jasmine = new Jasmine(); jasmine.loadConfigFile('spec/support/jasmine.json'); jasmine.configureDefaultReporter({ showColors: false }); jasmine.execute();