在不知道用什么方法来跑Jasmine测试的时候,突然想起来,我们可以用PhantomJS来跑Jasmine。
于是在谷歌了多次之后,总算写下了自己的gruntfile,主要依赖于
"grunt": "0.4.5",
"grunt-contrib-jasmine": "0.8.1",
"grunt-template-jasmine-requirejs": "0.2.0"
所以把上面的包添加到package.json
中。
然后创建一个gruntfile.js
module.exports = function (grunt) {
"use strict";
grunt.initConfig({
pkg: grunt.file.readJSON('package.json')
, jasmine: {
src: "web/test/lib/*.js"
, options: {
specs: "web/test/spec/*Spec.js",
template: require('grunt-template-jasmine-requirejs'),
templateOptions:{
requireConfig: {
baseUrl: './',
paths: {
'text': 'web/lib/text/text',
jquery: 'web/lib/jquery/dist/jquery.min',
json: 'web/lib/require/json',
router: 'web/router',
underscore: 'web/lib/underscore/underscore',
mustache: 'web/lib/mustache/mustache',
backbone: 'web/lib/backbone/backbone',
"jquery-cookie": "web/lib/jquery.cookie/jquery.cookie"
},
shim: {
"jquery-cookie": ["jquery"],
underscore: {
exports: '_'
}
}
}
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.registerTask('test', ['jasmine']);
grunt.registerTask('default', ['test']);
};
代码中比较重要的应该就是Requirejs相关的配置
template: require('grunt-template-jasmine-requirejs'),
templateOptions:{
requireConfig: {
baseUrl: './',
paths: {
'text': 'web/lib/text/text',
jquery: 'web/lib/jquery/dist/jquery.min',
json: 'web/lib/require/json',
router: 'web/router',
underscore: 'web/lib/underscore/underscore',
mustache: 'web/lib/mustache/mustache',
backbone: 'web/lib/backbone/backbone',
"jquery-cookie": "web/lib/jquery.cookie/jquery.cookie"
},
shim: {
"jquery-cookie": ["jquery"],
underscore: {
exports: '_'
}
}
}
}
注意:这里的baseUrl
可能会有问题,如果不注意的话
一开始用的是karma,结果报各种错,最后只好用phantomjs来跑
./node_modules/karma/bin/karma start karma.conf.js
INFO [karma]: Karma v0.12.24 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: Pattern "/Users/fdhuang/learing/freerice/web/test-main.js" does not match any file.
WARN [watcher]: Pattern "/Users/fdhuang/learing/freerice/web/test/lib/*.js" does not match any file.
INFO [Chrome 38.0.2125 (Mac OS X 10.10.0)]: Connected on socket Y_6khIJrAU_-klyONmnZ with id 89307968
Chrome 38.0.2125 (Mac OS X 10.10.0) ERROR
Uncaught Error: Mismatched anonymous define() module: function ($, UserSession) {
'use strict';
以及下面的一些配置问题
files: [
'test/test-main.js',
{pattern: 'test/lib/*.js', included: false},
{pattern: 'test/spec/*Spec.js', included: false}
],
围观我的Github Idea墙, 也许,你会遇到心仪的项目