Created
November 15, 2012 15:53
-
-
Save drydevelopment/4079340 to your computer and use it in GitHub Desktop.
Jasmine Console Runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jasmine.ConsoleReporter = function(showColors) | |
{ | |
if (!showColors) | |
showColors = true | |
var ansi = { | |
green: '\033[32m', | |
red: '\033[31m', | |
yellow: '\033[33m', | |
none: '\033[0m' | |
}, | |
language = { | |
spec: "spec", | |
failure: "failure" | |
}; | |
function coloredStr(color, str) | |
{ | |
return showColors ? (ansi[color] + str + ansi.none) : str; | |
} | |
function greenStr(str) | |
{ | |
return coloredStr("green", str); | |
} | |
function redStr(str) | |
{ | |
return coloredStr("red", str); | |
} | |
function yellowStr(str) | |
{ | |
return coloredStr("yellow", str); | |
} | |
function newline() | |
{ | |
console.log("\n"); | |
} | |
function started() | |
{ | |
console.log("Started"); | |
newline(); | |
} | |
function greenPass() | |
{ | |
console.log(greenStr("PASS")); | |
} | |
function redFail() | |
{ | |
console.log(redStr("FAIL")); | |
} | |
function yellowStar() | |
{ | |
console.log(yellowStr("*")); | |
} | |
function plural(str, count) | |
{ | |
return count == 1 ? str : str + "s"; | |
} | |
function repeat(thing, times) | |
{ | |
var arr = []; | |
for (var i = 0; i < times; i++) | |
{ | |
arr.push(thing); | |
} | |
return arr; | |
} | |
function indent(str, spaces) | |
{ | |
var lines = (str || '').split("\n"); | |
var newArr = []; | |
for (var i = 0; i < lines.length; i++) | |
{ | |
newArr.push(repeat(" ", spaces).join("") + lines[i]); | |
} | |
return newArr.join("\n"); | |
} | |
function specFailureDetails(suiteDescription, specDescription, stackTraces) | |
{ | |
newline(); | |
console.log(redStr(suiteDescription)); | |
console.log(redStr(indent(specDescription, 2))); | |
for (var i = 0; i < stackTraces.length; i++) | |
{ | |
console.log(indent(redStr(stackTraces[i]), 4)); | |
} | |
} | |
function finished(elapsed) | |
{ | |
newline(); | |
var finished = "Finished in " + elapsed / 1000 + " seconds"; | |
console.log(finished); | |
} | |
function summary(colorF, specs, failed) | |
{ | |
newline(); | |
console.log(colorF(specs + " " + plural(language.spec, specs) + ", " + | |
failed + " " + plural(language.failure, failed))); | |
} | |
function greenSummary(specs, failed) | |
{ | |
summary(greenStr, specs, failed); | |
} | |
function redSummary(specs, failed) | |
{ | |
summary(redStr, specs, failed); | |
} | |
function fullSuiteDescription(suite) | |
{ | |
var description = suite.description; | |
var currentSuite = suite; | |
while (currentSuite.parentSuite) | |
{ | |
currentSuite = currentSuite.parentSuite; | |
description = currentSuite.description + ': ' + description; | |
} | |
return description; | |
} | |
this.now = function() | |
{ | |
return new Date().getTime(); | |
}; | |
this.reportRunnerStarting = function() | |
{ | |
this.runnerStartTime = this.now(); | |
started(); | |
}; | |
this.reportSpecResults = function(spec) | |
{ | |
var results = spec.results(); | |
if (results.skipped) | |
{ | |
yellowStar(); | |
} | |
else | |
{ | |
var result = fullSuiteDescription(spec.suite); | |
if (results.passed()) | |
{ | |
result = greenStr(result); | |
} | |
else | |
{ | |
result = redStr(result); | |
} | |
console.log(result + ': ' + spec.description); | |
} | |
}; | |
this.suiteResults = []; | |
this.reportSuiteResults = function(suite) | |
{ | |
var suiteResult = { | |
description: fullSuiteDescription(suite), | |
failedSpecResults: [] | |
}; | |
suite.results().items_.forEach(function(spec) { | |
if (spec.failedCount > 0 && spec.description) | |
suiteResult.failedSpecResults.push(spec); | |
}); | |
this.suiteResults.push(suiteResult); | |
}; | |
function eachSpecFailure(suiteResults, callback) | |
{ | |
for (var i = 0; i < suiteResults.length; i++) | |
{ | |
var suiteResult = suiteResults[i]; | |
for (var j = 0; j < suiteResult.failedSpecResults.length; j++) | |
{ | |
var failedSpecResult = suiteResult.failedSpecResults[j]; | |
var stackTraces = []; | |
for (var k = 0; k < failedSpecResult.items_.length; k++) | |
{ | |
stackTraces.push(failedSpecResult.items_[k].trace.stack); | |
} | |
callback(suiteResult.description, failedSpecResult.description, stackTraces); | |
} | |
} | |
}; | |
this.reportRunnerResults = function(runner) { | |
eachSpecFailure(this.suiteResults, function(suiteDescription, specDescription, stackTraces) { | |
specFailureDetails(suiteDescription, specDescription, stackTraces); | |
}); | |
finished(this.now() - this.runnerStartTime); | |
var results = runner.results(); | |
var summaryFunction = results.failedCount === 0 ? greenSummary : redSummary; | |
summaryFunction(runner.specs().length, results.failedCount); | |
this.status = (results.failedCount > 0)? "fail" : "success"; | |
console.log(""); | |
console.log("ConsoleReporter finished"); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment