2012-11-02 13:03:09 +08:00
|
|
|
/*
|
|
|
|
* grunt-contrib-jasmine
|
|
|
|
* http://gruntjs.com/
|
|
|
|
*
|
|
|
|
* Licensed under the MIT license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module.exports = function(grunt) {
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
// node api
|
|
|
|
var fs = require('fs'),
|
2013-01-09 03:28:26 +08:00
|
|
|
path = require('path');
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
// npm lib
|
|
|
|
var phantomjs = require('grunt-lib-phantomjs').init(grunt);
|
|
|
|
|
|
|
|
// local lib
|
2013-02-06 06:13:10 +08:00
|
|
|
var jasmine = require('./lib/jasmine').init(grunt, phantomjs);
|
2013-01-09 03:28:26 +08:00
|
|
|
|
|
|
|
var junitTemplate = __dirname + '/jasmine/templates/JUnit.tmpl';
|
2012-11-02 13:03:09 +08:00
|
|
|
|
2013-03-30 06:18:51 +08:00
|
|
|
var status = {};
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
grunt.registerMultiTask('jasmine', 'Run jasmine specs headlessly through PhantomJS.', function() {
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
// Merge task-specific options with these defaults.
|
2012-11-02 13:03:09 +08:00
|
|
|
var options = this.options({
|
2013-03-01 22:58:22 +08:00
|
|
|
version : '1.3.1',
|
2012-11-02 13:03:09 +08:00
|
|
|
timeout : 10000,
|
2013-03-11 13:15:29 +08:00
|
|
|
styles : [],
|
2012-11-02 13:03:09 +08:00
|
|
|
specs : [],
|
|
|
|
helpers : [],
|
|
|
|
vendor : [],
|
|
|
|
outfile : '_SpecRunner.html',
|
|
|
|
host : '',
|
2013-01-09 03:28:26 +08:00
|
|
|
template : __dirname + '/jasmine/templates/DefaultRunner.tmpl',
|
2012-11-02 13:03:09 +08:00
|
|
|
templateOptions : {},
|
2013-01-08 07:04:44 +08:00
|
|
|
junit: {}
|
2012-11-02 13:03:09 +08:00
|
|
|
});
|
|
|
|
|
2013-01-10 13:14:50 +08:00
|
|
|
if (options.template === 'requirejs') {
|
|
|
|
grunt.log.warn(
|
|
|
|
'The requirejs template is no longer included in grunt-contrib-jasmine core.\n' +
|
|
|
|
'Please see the https://github.com/gruntjs/grunt-contrib-jasmine README for details'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
if (grunt.option('debug')) {
|
2012-11-02 13:03:09 +08:00
|
|
|
grunt.log.debug(options);
|
2012-11-02 13:03:09 +08:00
|
|
|
}
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
setup(options);
|
|
|
|
|
2013-06-19 11:26:34 +08:00
|
|
|
// The filter returned no spec, let's skip phantom.
|
|
|
|
if(!jasmine.buildSpecrunner(this.filesSrc, options)) {
|
|
|
|
return removePhantomListeners();
|
|
|
|
}
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
// If we're just building (e.g. for web), skip phantom.
|
|
|
|
if (this.flags.build) return;
|
|
|
|
|
|
|
|
var done = this.async();
|
|
|
|
phantomRunner(options, function(err,status) {
|
2013-03-11 23:08:43 +08:00
|
|
|
var success = !err && status.failed === 0;
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
if (err) grunt.log.error(err);
|
|
|
|
if (status.failed === 0) grunt.log.ok('0 failures');
|
|
|
|
else grunt.log.error(status.failed + ' failures');
|
2013-03-11 23:08:43 +08:00
|
|
|
|
2013-06-16 02:50:37 +08:00
|
|
|
teardown(options, function(){
|
|
|
|
done(success);
|
|
|
|
});
|
2012-11-02 13:03:09 +08:00
|
|
|
});
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-02-24 09:14:11 +08:00
|
|
|
function logWrite(text, isInline) {
|
|
|
|
text += (isInline ? '' : '\n');
|
|
|
|
status.log += text;
|
|
|
|
grunt.verbose.write(text);
|
|
|
|
}
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
function phantomRunner(options,cb){
|
|
|
|
var file = options.outfile;
|
|
|
|
|
|
|
|
if (options.host) {
|
2013-04-05 01:39:00 +08:00
|
|
|
if (!(/\/$/).test(options.host)) options.host = options.host + '/';
|
2012-11-02 13:03:09 +08:00
|
|
|
file = options.host + options.outfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
grunt.verbose.subhead('Testing jasmine specs via phantom').or.writeln('Testing jasmine specs via phantom');
|
|
|
|
|
|
|
|
phantomjs.spawn(file, {
|
|
|
|
failCode : 90,
|
|
|
|
options : options,
|
|
|
|
done : function(err){
|
|
|
|
cb(err,status);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-16 02:50:37 +08:00
|
|
|
function teardown(options, cb) {
|
2013-06-19 11:26:34 +08:00
|
|
|
removePhantomListeners();
|
2013-06-16 02:50:37 +08:00
|
|
|
|
|
|
|
if (!options.keepRunner && fs.statSync(options.outfile).isFile()) fs.unlink(options.outfile);
|
2013-06-18 18:17:24 +08:00
|
|
|
if (!options.keepRunner) {
|
|
|
|
jasmine.cleanTemp(cb);
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
2012-11-02 13:03:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-19 11:26:34 +08:00
|
|
|
function removePhantomListeners() {
|
|
|
|
phantomjs.removeAllListeners();
|
|
|
|
phantomjs.listenersAny().length = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
function setup(options) {
|
|
|
|
var thisRun = {};
|
|
|
|
|
2013-03-30 06:18:51 +08:00
|
|
|
status = {
|
|
|
|
specs : 0,
|
|
|
|
failed : 0,
|
|
|
|
passed : 0,
|
|
|
|
total : 0,
|
|
|
|
skipped : 0,
|
|
|
|
duration : 0,
|
|
|
|
log : ''
|
|
|
|
};
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
phantomjs.on('fail.timeout',function(){
|
|
|
|
grunt.log.writeln();
|
|
|
|
grunt.warn('PhantomJS timed out, possibly due to an unfinished async spec.', 90);
|
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('console',console.log.bind(console));
|
|
|
|
phantomjs.on('verbose',grunt.verbose.writeln.bind(grunt.verbose));
|
|
|
|
phantomjs.on('debug', grunt.log.debug.bind(grunt.log, 'phantomjs'));
|
|
|
|
phantomjs.on('write', grunt.log.write.bind(grunt.log));
|
|
|
|
phantomjs.on('writeln', grunt.log.writeln.bind(grunt.log));
|
|
|
|
phantomjs.on('error.onError',function(string, trace){
|
2013-03-09 10:04:51 +08:00
|
|
|
if (trace && trace.length) {
|
2012-11-02 13:03:09 +08:00
|
|
|
grunt.log.error(string.red + ' at ');
|
|
|
|
trace.forEach(function(line) {
|
|
|
|
var file = line.file.replace(/^file:/,'');
|
|
|
|
var message = grunt.util._('%s:%d %s').sprintf(path.relative('.',file), line.line, line.function);
|
|
|
|
grunt.log.error(message.red);
|
|
|
|
});
|
|
|
|
} else {
|
2013-06-16 03:08:34 +08:00
|
|
|
grunt.log.error("Error caught from phantom. More info can be found by opening the Spec Runner in a browser.");
|
|
|
|
grunt.warn(string);
|
2012-11-02 13:03:09 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.onAny(function() {
|
|
|
|
var args = [this.event].concat(grunt.util.toArray(arguments));
|
|
|
|
grunt.event.emit.apply(grunt.event, args);
|
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('jasmine.reportRunnerStarting',function(suites) {
|
|
|
|
grunt.verbose.writeln('Starting...');
|
|
|
|
thisRun.start_time = (new Date()).getTime();
|
|
|
|
thisRun.executed_specs = 0;
|
|
|
|
thisRun.passed_specs = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('jasmine.reportSpecStarting',function(spec) {
|
|
|
|
thisRun.executed_specs++;
|
|
|
|
grunt.verbose.write(spec.suite.description + ':' + spec.description + '...');
|
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('jasmine.reportSuiteResults',function(suite){
|
|
|
|
//grunt.verbose.writeln(suite.description + ": " + suite.results.passedCount + " of " + suite.results.totalCount + " passed.");
|
|
|
|
});
|
|
|
|
|
2013-02-24 09:14:11 +08:00
|
|
|
phantomjs.on('jasmine.reportSpecResults',function(specId, result, fullName) {
|
2012-11-02 13:03:09 +08:00
|
|
|
if (result.passed) thisRun.passed_specs++;
|
|
|
|
|
2013-02-24 09:14:11 +08:00
|
|
|
if (!result.passed) {
|
|
|
|
if (grunt.option('verbose'))
|
|
|
|
grunt.verbose.writeln(result.description + ': ' + result.msg.red);
|
|
|
|
else {
|
|
|
|
logWrite(fullName + ': ' + result.msg.red);
|
|
|
|
grunt.log.write('x'.red);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
grunt.verbose.writeln(result.description + ': ' + result.msg.green);
|
|
|
|
if (!grunt.option('verbose'))
|
|
|
|
grunt.log.write('.');
|
|
|
|
}
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
for (var i = 0; i < result.messages.length; i++) {
|
|
|
|
var item = result.messages[i];
|
|
|
|
|
|
|
|
if (item.type === 'log') {
|
|
|
|
grunt.verbose.writeln(item.toString());
|
|
|
|
} else if (item.type === 'expect' && !item.passed_) {
|
2013-02-24 09:14:11 +08:00
|
|
|
var specIndex = ' ('+(i+1)+')';
|
|
|
|
logWrite(' ' + item.message.red+specIndex.red);
|
2012-11-02 13:03:09 +08:00
|
|
|
phantomjs.emit('onError', item.message, item.trace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
phantomjs.emit( 'jasmine.testDone', result.totalCount, result.passedCount, result.failedCount, result.skipped );
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('jasmine.reportRunnerResults',function(){
|
|
|
|
var dur = (new Date()).getTime() - thisRun.start_time;
|
2013-02-24 09:05:32 +08:00
|
|
|
var spec_str = thisRun.executed_specs + (thisRun.executed_specs === 1 ? " spec " : " specs ");
|
2013-06-16 00:38:39 +08:00
|
|
|
grunt.verbose.writeln('Runner finished');
|
|
|
|
if (thisRun.executed_specs === 0) {
|
|
|
|
grunt.warn('No specs executed, is there a configuration error?');
|
|
|
|
}
|
2013-02-24 09:14:11 +08:00
|
|
|
if (!grunt.option('verbose')) {
|
|
|
|
grunt.log.writeln('');
|
|
|
|
grunt.log.write(status.log);
|
|
|
|
}
|
2013-02-24 09:05:32 +08:00
|
|
|
grunt.log.writeln(spec_str + 'in ' + (dur/1000) + "s.");
|
2012-11-02 13:03:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('jasmine.testDone',function(totalAssertions, passedAssertions, failedAssertions, skippedAssertions){
|
|
|
|
status.specs++;
|
|
|
|
status.failed += failedAssertions;
|
|
|
|
status.passed += passedAssertions;
|
|
|
|
status.total += totalAssertions;
|
|
|
|
status.skipped += skippedAssertions;
|
|
|
|
});
|
|
|
|
|
2013-01-08 07:04:44 +08:00
|
|
|
phantomjs.on('jasmine.reportJUnitResults',function(junitData){
|
|
|
|
if (options.junit && options.junit.path) {
|
2013-01-09 03:28:26 +08:00
|
|
|
var template = grunt.file.read(junitTemplate);
|
2013-01-08 21:09:05 +08:00
|
|
|
if (options.junit.consolidate) {
|
2013-01-09 03:28:26 +08:00
|
|
|
grunt.util._(junitData.consolidatedSuites).each(function(suites){
|
|
|
|
var xmlFile = path.join(options.junit.path, 'TEST-' + suites[0].name.replace(/[^\w]/g, '') + '.xml');
|
|
|
|
grunt.file.write(xmlFile, grunt.util._.template(template, { testsuites: suites}));
|
|
|
|
});
|
2013-01-08 21:09:05 +08:00
|
|
|
} else {
|
2013-01-09 03:28:26 +08:00
|
|
|
junitData.suites.forEach(function(suiteData){
|
|
|
|
var xmlFile = path.join(options.junit.path, 'TEST-' + suiteData.name.replace(/[^\w]/g, '') + '.xml');
|
|
|
|
grunt.file.write(xmlFile, grunt.util._.template(template, { testsuites: [suiteData] }));
|
|
|
|
});
|
2013-01-08 21:09:05 +08:00
|
|
|
}
|
2013-01-08 07:04:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
phantomjs.on('jasmine.done',function(elapsed){
|
|
|
|
phantomjs.halt();
|
|
|
|
status.duration = elapsed;
|
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('jasmine.done.PhantomReporter',function(){
|
|
|
|
phantomjs.emit('jasmine.done');
|
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('jasmine.done_fail',function(url){
|
|
|
|
grunt.log.error();
|
|
|
|
grunt.warn('PhantomJS unable to load "' + url + '" URI.', 90);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-03-30 06:18:51 +08:00
|
|
|
};
|