|
| 1 | +/** |
| 2 | + Jasmine Reporter that outputs test results to the browser console. |
| 3 | + Useful for running in a headless environment such as PhantomJs, ZombieJs etc. |
| 4 | +
|
| 5 | + Usage: |
| 6 | + // From your html file that loads jasmine: |
| 7 | + jasmine.getEnv().addReporter(new jasmine.ConsoleReporter()); |
| 8 | + jasmine.getEnv().execute(); |
| 9 | +*/ |
| 10 | + |
| 11 | +(function(jasmine, console) { |
| 12 | + if (!jasmine) { |
| 13 | + throw "jasmine library isn't loaded!"; |
| 14 | + } |
| 15 | + |
| 16 | + var ANSI = {} |
| 17 | + ANSI.color_map = { |
| 18 | + "green" : 32, |
| 19 | + "red" : 31 |
| 20 | + } |
| 21 | + |
| 22 | + ANSI.colorize_text = function(text, color) { |
| 23 | + var color_code = this.color_map[color]; |
| 24 | + return "\033[" + color_code + "m" + text + "\033[0m"; |
| 25 | + } |
| 26 | + |
| 27 | + var ConsoleReporter = function() { |
| 28 | + if (!console || !console.log) { throw "console isn't present!"; } |
| 29 | + this.status = this.statuses.stopped; |
| 30 | + }; |
| 31 | + |
| 32 | + var proto = ConsoleReporter.prototype; |
| 33 | + proto.statuses = { |
| 34 | + stopped : "stopped", |
| 35 | + running : "running", |
| 36 | + fail : "fail", |
| 37 | + success : "success" |
| 38 | + }; |
| 39 | + |
| 40 | + proto.reportRunnerStarting = function(runner) { |
| 41 | + this.status = this.statuses.running; |
| 42 | + this.start_time = (new Date()).getTime(); |
| 43 | + this.executed_specs = 0; |
| 44 | + this.passed_specs = 0; |
| 45 | + this.log("Starting..."); |
| 46 | + }; |
| 47 | + |
| 48 | + proto.reportRunnerResults = function(runner) { |
| 49 | + var failed = this.executed_specs - this.passed_specs; |
| 50 | + var spec_str = this.executed_specs + (this.executed_specs === 1 ? " spec, " : " specs, "); |
| 51 | + var fail_str = failed + (failed === 1 ? " failure in " : " failures in "); |
| 52 | + var color = (failed > 0)? "red" : "green"; |
| 53 | + var dur = (new Date()).getTime() - this.start_time; |
| 54 | + |
| 55 | + this.log(""); |
| 56 | + this.log("Finished"); |
| 57 | + this.log("-----------------"); |
| 58 | + this.log(spec_str + fail_str + (dur/1000) + "s.", color); |
| 59 | + |
| 60 | + this.status = (failed > 0)? this.statuses.fail : this.statuses.success; |
| 61 | + |
| 62 | + /* Print something that signals that testing is over so that headless browsers |
| 63 | + like PhantomJs know when to terminate. */ |
| 64 | + this.log(""); |
| 65 | + this.log("ConsoleReporter finished"); |
| 66 | + }; |
| 67 | + |
| 68 | + |
| 69 | + proto.reportSpecStarting = function(spec) { |
| 70 | + this.executed_specs++; |
| 71 | + }; |
| 72 | + |
| 73 | + proto.reportSpecResults = function(spec) { |
| 74 | + if (spec.results().passed()) { |
| 75 | + this.passed_specs++; |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + var resultText = spec.suite.description + " : " + spec.description; |
| 80 | + this.log(resultText, "red"); |
| 81 | + |
| 82 | + var items = spec.results().getItems() |
| 83 | + for (var i = 0; i < items.length; i++) { |
| 84 | + var trace = items[i].trace.stack || items[i].trace; |
| 85 | + this.log(trace, "red"); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + proto.reportSuiteResults = function(suite) { |
| 90 | + if (!suite.parentSuite) { return; } |
| 91 | + var results = suite.results(); |
| 92 | + var failed = results.totalCount - results.passedCount; |
| 93 | + var color = (failed > 0)? "red" : "green"; |
| 94 | + this.log(suite.description + ": " + results.passedCount + " of " + results.totalCount + " passed.", color); |
| 95 | + }; |
| 96 | + |
| 97 | + proto.log = function(str, color) { |
| 98 | + var text = (color != undefined)? ANSI.colorize_text(str, color) : str; |
| 99 | + console.log(text) |
| 100 | + }; |
| 101 | + |
| 102 | + jasmine.ConsoleReporter = ConsoleReporter; |
| 103 | +})(jasmine, console); |
0 commit comments