Skip to content

Commit

Permalink
Make specs emit TAP ("Test Anything Protocol") output to console
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSanderson committed Mar 26, 2013
1 parent 1a76f5e commit c9b5251
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
96 changes: 96 additions & 0 deletions spec/lib/jasmine-1.2.0/jasmine-tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// This code from https://github.com/larrymyers/jasmine-reporters
// License: MIT (https://github.com/larrymyers/jasmine-reporters/blob/master/LICENSE)

(function() {
if (! jasmine) {
throw new Exception("jasmine library does not exist in global namespace!");
}

/**
* TAP (http://en.wikipedia.org/wiki/Test_Anything_Protocol) reporter.
* outputs spec results to the console.
*
* Heavily inspired by ConsoleReporter found at:
* https://github.com/larrymyers/jasmine-reporters/
*
* Usage:
*
* jasmine.getEnv().addReporter(new jasmine.TapReporter());
* jasmine.getEnv().execute();
*/
var TapReporter = function() {
this.started = false;
this.finished = false;
};

TapReporter.prototype = {

reportRunnerStarting: function(runner) {
this.started = true;
this.start_time = (new Date()).getTime();
this.executed_specs = 0;
this.passed_specs = 0;
this.executed_asserts = 0;
this.passed_asserts = 0;
// should have at least 1 spec, otherwise it's considered a failure
this.log('1..'+ Math.max(runner.specs().length, 1));
},

reportSpecStarting: function(spec) {
this.executed_specs++;
},

reportSpecResults: function(spec) {
var resultText = "not ok";
var errorMessage = '';

var results = spec.results();
var passed = results.passed();

this.passed_asserts += results.passedCount;
this.executed_asserts += results.totalCount;

if (passed) {
this.passed_specs++;
resultText = "ok";
} else {
var items = results.getItems();
var i = 0;
var expectationResult, stackMessage;
while (expectationResult = items[i++]) {
if (expectationResult.trace) {
stackMessage = expectationResult.trace.stack? expectationResult.trace.stack : expectationResult.message;
errorMessage += '\n '+ stackMessage;
}
}
}

this.log(resultText +" "+ (spec.id + 1) +" - "+ spec.suite.description +" : "+ spec.description + errorMessage);
},

reportRunnerResults: function(runner) {
var dur = (new Date()).getTime() - this.start_time;
var failed = this.executed_specs - this.passed_specs;
var spec_str = this.executed_specs + (this.executed_specs === 1 ? " spec, " : " specs, ");
var fail_str = failed + (failed === 1 ? " failure in " : " failures in ");
var assert_str = this.executed_asserts + (this.executed_asserts === 1 ? " assertion, " : " assertions, ");

if (this.executed_asserts) {
this.log("# "+ spec_str + assert_str + fail_str + (dur/1000) + "s.");
} else {
this.log('not ok 1 - no asserts run.');
}
this.finished = true;
},

log: function(str) {
var console = jasmine.getGlobal().console;
if (console && console.log) {
console.log(str);
}
}
};

// export public
jasmine.TapReporter = TapReporter;
})();
2 changes: 2 additions & 0 deletions spec/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<link rel="stylesheet" type="text/css" href="lib/jasmine-1.2.0/jasmine.css" />
<script type="text/javascript" src="lib/jasmine-1.2.0/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-1.2.0/jasmine-html.js"></script>
<script type="text/javascript" src="lib/jasmine-1.2.0/jasmine-tap.js"></script>

<!-- our jasmine extensions -->
<link rel="stylesheet" type="text/css" href="lib/jasmine.extensions.css" />
Expand Down Expand Up @@ -79,6 +80,7 @@
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = htmlReporter.specFilter;

jasmineEnv.addReporter(new jasmine.TapReporter()); // For Testling CI
jasmineEnv.execute();

})();
Expand Down

0 comments on commit c9b5251

Please sign in to comment.