Jasmine是一个很好的单元测试框架,它有漂亮简单的API
describe('you can group test cases in "describe" blocks...', function() { describe('...which can also be nested', function() { it('test cases are in "it" blocks', function() { var string = 'where we can run arbitrary JavaScript code...'; // ...and make assertions about results using "expect": expect(string).toEqual('expected string'); }); }); });
Karma 是一个集成了像 Jasmine(基于 BDD 的测试框架),PhantomJS(无界面的浏览器)等的测试工具。
npm安装好后,就要写karma的配置文件
//karma.conf.js module.exports = function(config) { config.set({ frameworks: ['jasmine'], files: [ 'src/**/*.js', 'test/**/*_spec.js' ], preprocessors: { 'test/**/*.js': ['jshint','browserify'],
'src/**/*.js': ['jshint','browserify']
},
/**
** ChromeDebugging,ChromeDebugging可以打开chrome devtool,出来的画面,点击DEBUG按钮,调试测试用例,
** PhantomJS需要安装对应的phantomjs和karma-phantomjs-laucher
*/
browsers: ['ChromeDebugging'],
customLaunchers: {
ChromeDebugging: {
base: 'Chrome',
flags: ['--remote-debugging-port=9333']
}
}) }
这里定义了要测试的文件路径,同时定义了跑测试前,一个jshint的预处理。(触发JSHint)
//.jshintrc { "browser": true, "browserify": true, "devel": true, "globals": { "jasmine": false, "describe": false, "it": false, "expect": false, "beforeEach": false, "afterEach": false } }