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'),
|
2014-01-29 09:12:51 +08:00
|
|
|
path = require('path');
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
// npm lib
|
2014-01-29 09:12:51 +08:00
|
|
|
var phantomjs = require('grunt-lib-phantomjs').init(grunt),
|
|
|
|
chalk = require('chalk'),
|
|
|
|
_ = require('lodash');
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
var symbols = {
|
|
|
|
check : '✓',
|
|
|
|
error : 'X',
|
|
|
|
splat : '*'
|
|
|
|
};
|
|
|
|
|
|
|
|
//With node.js on Windows: use symbols available in terminal default fonts
|
|
|
|
//https://github.com/visionmedia/mocha/pull/641
|
|
|
|
if (process && process.platform === 'win32') {
|
|
|
|
symbols = {
|
|
|
|
check : '\u221A',
|
|
|
|
error : '\u00D7',
|
|
|
|
splat : '*'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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({
|
2014-01-29 09:12:51 +08:00
|
|
|
version : '2.0.0',
|
2012-11-02 13:03:09 +08:00
|
|
|
timeout : 10000,
|
2014-01-29 09:12:51 +08:00
|
|
|
styles : [],
|
|
|
|
specs : [],
|
2012-11-02 13:03:09 +08:00
|
|
|
helpers : [],
|
2014-01-29 09:12:51 +08:00
|
|
|
vendor : [],
|
2012-11-02 13:03:09 +08:00
|
|
|
outfile : '_SpecRunner.html',
|
2014-01-29 09:12:51 +08:00
|
|
|
host : '',
|
2013-01-09 03:28:26 +08:00
|
|
|
template : __dirname + '/jasmine/templates/DefaultRunner.tmpl',
|
2012-11-02 13:03:09 +08:00
|
|
|
templateOptions : {},
|
2014-01-29 09:12:51 +08:00
|
|
|
junit : {},
|
|
|
|
ignoreEmpty: grunt.option('force') === true
|
|
|
|
});
|
2013-01-10 13:14:50 +08:00
|
|
|
|
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);
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
// The filter returned no spec files so skip phantom.
|
|
|
|
if (!jasmine.buildSpecrunner(this.filesSrc, options)) {
|
2013-06-19 11:26:34 +08:00
|
|
|
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();
|
2014-01-29 09:12:51 +08:00
|
|
|
phantomRunner(options, function(err, status) {
|
2013-03-11 23:08:43 +08:00
|
|
|
var success = !err && status.failed === 0;
|
|
|
|
|
2014-01-29 09:12:51 +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
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
teardown(options, function() {
|
2013-06-16 02:50:37 +08:00
|
|
|
done(success);
|
|
|
|
});
|
2012-11-02 13:03:09 +08:00
|
|
|
});
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
function phantomRunner(options, cb){
|
2012-11-02 13:03:09 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
grunt.verbose.subhead('Testing jasmine specs via phantom').or.writeln('Testing jasmine specs via PhantomJS');
|
|
|
|
grunt.log.writeln('');
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
phantomjs.spawn(file, {
|
|
|
|
failCode : 90,
|
2014-01-29 09:12:51 +08:00
|
|
|
options : options,
|
|
|
|
done : function(err){
|
2012-11-02 13:03:09 +08:00
|
|
|
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
|
|
|
|
2014-01-29 09:12:51 +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) {
|
2014-01-29 09:12:51 +08:00
|
|
|
var indentLevel = 1,
|
|
|
|
tabstop = 2,
|
|
|
|
thisRun = {},
|
|
|
|
suites = {},
|
|
|
|
currentSuite;
|
2012-11-02 13:03:09 +08:00
|
|
|
|
2013-03-30 06:18:51 +08:00
|
|
|
status = {
|
2014-01-29 09:12:51 +08:00
|
|
|
failed : 0
|
2013-03-30 06:18:51 +08:00
|
|
|
};
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
function indent(times) {
|
|
|
|
return new Array(+times * tabstop).join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
phantomjs.on('fail.timeout', function() {
|
2012-11-02 13:03:09 +08:00
|
|
|
grunt.log.writeln();
|
|
|
|
grunt.warn('PhantomJS timed out, possibly due to an unfinished async spec.', 90);
|
|
|
|
});
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
phantomjs.on('console', function(msg) {
|
2014-01-31 04:26:29 +08:00
|
|
|
thisRun.cleanConsole = false;
|
2014-01-29 09:12:51 +08:00
|
|
|
grunt.log.writeln('\n' + chalk.yellow('log: ') + msg);
|
2013-11-21 08:50:53 +08:00
|
|
|
});
|
2014-01-29 09:12:51 +08:00
|
|
|
|
|
|
|
phantomjs.on('error.onError', function(string, trace){
|
2013-03-09 10:04:51 +08:00
|
|
|
if (trace && trace.length) {
|
2014-01-29 09:12:51 +08:00
|
|
|
grunt.log.error(chalk.red(string) + ' at ');
|
2012-11-02 13:03:09 +08:00
|
|
|
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);
|
2014-01-29 09:12:51 +08:00
|
|
|
grunt.log.error(chalk.red(message));
|
2012-11-02 13:03:09 +08:00
|
|
|
});
|
|
|
|
} else {
|
2014-01-29 09:12:51 +08:00
|
|
|
grunt.log.error("Error caught from PhantomJS. More info can be found by opening the Spec Runner in a browser.");
|
2013-06-16 03:08:34 +08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
phantomjs.on('jasmine.jasmineStarted', function() {
|
|
|
|
grunt.verbose.writeln('Jasmine Runner Starting...');
|
|
|
|
thisRun.startTime = (new Date()).getTime();
|
|
|
|
thisRun.executedSpecs = 0;
|
|
|
|
thisRun.passedSpecs = 0;
|
|
|
|
thisRun.failedSpecs = 0;
|
|
|
|
thisRun.skippedSpecs = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('jasmine.suiteStarted', function(suiteMetaData) {
|
|
|
|
currentSuite = suiteMetaData.id;
|
|
|
|
suites[currentSuite] = {
|
|
|
|
name : suiteMetaData.fullName,
|
|
|
|
timestamp : new Date(suiteMetaData.startTime),
|
|
|
|
errors : 0,
|
|
|
|
tests : 0,
|
|
|
|
failures : 0,
|
|
|
|
testcases : []
|
|
|
|
};
|
|
|
|
grunt.log.write(indent(indentLevel++));
|
|
|
|
grunt.log.writeln(chalk.bold(suiteMetaData.description));
|
2012-11-02 13:03:09 +08:00
|
|
|
});
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
phantomjs.on('jasmine.suiteDone', function(suiteMetaData) {
|
|
|
|
suites[currentSuite].time = suiteMetaData.duration / 1000;
|
|
|
|
indentLevel--;
|
2012-11-02 13:03:09 +08:00
|
|
|
});
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
phantomjs.on('jasmine.specStarted', function(specMetaData) {
|
|
|
|
thisRun.executedSpecs++;
|
2014-01-31 04:26:29 +08:00
|
|
|
thisRun.cleanConsole = true;
|
|
|
|
grunt.log.write(indent(indentLevel) + '- ' + chalk.grey(specMetaData.description) + '...');
|
2014-01-29 09:12:51 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
phantomjs.on('jasmine.specDone', function(specMetaData) {
|
|
|
|
var specSummary = {
|
|
|
|
assertions : 0,
|
|
|
|
classname : suites[currentSuite].name,
|
|
|
|
name : specMetaData.description,
|
|
|
|
time : specMetaData.duration / 1000,
|
|
|
|
failureMessages : []
|
|
|
|
};
|
|
|
|
|
|
|
|
suites[currentSuite].tests++;
|
|
|
|
|
|
|
|
var color = 'yellow',
|
|
|
|
symbol = 'splat';
|
|
|
|
if (specMetaData.status === "passed") {
|
|
|
|
thisRun.passedSpecs++;
|
|
|
|
color = 'green';
|
|
|
|
symbol = 'check';
|
|
|
|
} else if (specMetaData.status === "failed") {
|
|
|
|
thisRun.failedSpecs++;
|
|
|
|
status.failed++;
|
|
|
|
color = 'red';
|
|
|
|
symbol = 'error';
|
|
|
|
suites[currentSuite].failures++;
|
|
|
|
suites[currentSuite].errors += specMetaData.failedExpectations.length;
|
|
|
|
specSummary.failureMessages = specMetaData.failedExpectations.map(function(error){
|
|
|
|
return error.message;
|
|
|
|
});
|
2013-02-24 09:14:11 +08:00
|
|
|
} else {
|
2014-01-29 09:12:51 +08:00
|
|
|
thisRun.skippedSpecs++;
|
2013-02-24 09:14:11 +08:00
|
|
|
}
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
suites[currentSuite].testcases.push(specSummary);
|
|
|
|
|
2014-01-31 04:26:29 +08:00
|
|
|
// If we're writing to a proper terminal, make it fancy.
|
|
|
|
if (process.stdout.clearLine) {
|
|
|
|
process.stdout.clearLine();
|
|
|
|
process.stdout.cursorTo(0);
|
|
|
|
grunt.log.writeln(
|
|
|
|
indent(indentLevel) +
|
|
|
|
chalk[color].bold(symbols[symbol]) + ' ' +
|
|
|
|
chalk.grey(specMetaData.description)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// If we haven't written out since we've started
|
|
|
|
if (thisRun.cleanConsole) {
|
|
|
|
// then append to the current line.
|
|
|
|
grunt.log.writeln('...' + symbols[symbol]);
|
|
|
|
} else {
|
|
|
|
// Otherwise reprint the current spec and status.
|
|
|
|
grunt.log.writeln(
|
|
|
|
indent(indentLevel) + '...' +
|
|
|
|
chalk.grey(specMetaData.description) + '...' +
|
|
|
|
symbols[symbol]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 13:03:09 +08:00
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
specMetaData.failedExpectations.forEach(function(error, i){
|
2013-10-26 08:45:39 +08:00
|
|
|
var specIndex = ' ('+(i+1)+')';
|
2014-01-29 09:12:51 +08:00
|
|
|
grunt.log.writeln(indent(indentLevel + 1) + chalk.red(error.message + specIndex));
|
|
|
|
phantomjs.emit('onError', error.message, error.stack);
|
|
|
|
});
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
});
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
phantomjs.on('jasmine.jasmineDone', function(){
|
|
|
|
var dur = (new Date()).getTime() - thisRun.startTime;
|
|
|
|
var specQuantity = thisRun.executedSpecs + (thisRun.executedSpecs === 1 ? " spec " : " specs ");
|
|
|
|
|
|
|
|
grunt.verbose.writeln('Jasmine runner finished');
|
|
|
|
|
|
|
|
if (thisRun.executedSpecs === 0) {
|
|
|
|
// log.error will print the message but not fail the task, warn will do both.
|
|
|
|
var log = (options.ignoreEmpty) ? grunt.log.error : grunt.warn;
|
|
|
|
|
|
|
|
log('No specs executed, is there a configuration error?');
|
2013-06-16 00:38:39 +08:00
|
|
|
}
|
2014-01-29 09:12:51 +08:00
|
|
|
|
|
|
|
if (options.junit && options.junit.path) {
|
|
|
|
writeJunitXml(suites);
|
2013-02-24 09:14:11 +08:00
|
|
|
}
|
2012-11-02 13:03:09 +08:00
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
grunt.log.writeln('\n' + specQuantity + 'in ' + (dur / 1000) + "s.");
|
2012-11-02 13:03:09 +08:00
|
|
|
});
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
function writeJunitXml(testsuites){
|
|
|
|
var template = grunt.file.read(junitTemplate);
|
|
|
|
if (options.junit.consolidate) {
|
|
|
|
var xmlFile = path.join(options.junit.path, 'TEST-' + testsuites.suite1.name.replace(/[^\w]/g, '') + '.xml');
|
|
|
|
grunt.file.write(xmlFile, grunt.util._.template(template, { testsuites: _.values(testsuites)}));
|
|
|
|
} else {
|
|
|
|
_.forEach(testsuites, function(suiteData){
|
|
|
|
var xmlFile = path.join(options.junit.path, 'TEST-' + suiteData.name.replace(/[^\w]/g, '') + '.xml');
|
|
|
|
grunt.file.write(xmlFile, _.template(template, { testsuites: [suiteData] }));
|
|
|
|
});
|
2013-01-08 07:04:44 +08:00
|
|
|
}
|
2014-01-29 09:12:51 +08:00
|
|
|
}
|
2013-01-08 07:04:44 +08:00
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
phantomjs.on('jasmine.done', function(elapsed) {
|
2012-11-02 13:03:09 +08:00
|
|
|
phantomjs.halt();
|
|
|
|
});
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
phantomjs.on('jasmine.done.PhantomReporter', function() {
|
2012-11-02 13:03:09 +08:00
|
|
|
phantomjs.emit('jasmine.done');
|
|
|
|
});
|
|
|
|
|
2014-01-29 09:12:51 +08:00
|
|
|
phantomjs.on('jasmine.done_fail', function(url) {
|
2012-11-02 13:03:09 +08:00
|
|
|
grunt.log.error();
|
|
|
|
grunt.warn('PhantomJS unable to load "' + url + '" URI.', 90);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-03-30 06:18:51 +08:00
|
|
|
};
|