Report spec results properly.

This commit is contained in:
Christian Williams & Aaron Peckham 2009-07-10 14:35:24 -07:00
parent 286fc25aab
commit 5b695d3deb
6 changed files with 150 additions and 14 deletions

View File

@ -29,7 +29,7 @@ module Jasmine
class SimpleServer class SimpleServer
def self.start(port, spec_dir, mappings) def self.start(port, spec_dir, mappings)
require "thin" require 'thin'
config = { config = {
'/run.html' => Jasmine::RunAdapter.new(spec_dir) '/run.html' => Jasmine::RunAdapter.new(spec_dir)
@ -83,7 +83,8 @@ module Jasmine
def eval_js(script) def eval_js(script)
escaped_script = "'" + script.gsub(/(['\\])/) { '\\' + $1 } + "'" escaped_script = "'" + script.gsub(/(['\\])/) { '\\' + $1 } + "'"
@driver.get_eval("window.eval(#{escaped_script})") result = @driver.get_eval("window.eval(#{escaped_script})")
JSON.parse("[#{result}]")[0]
end end
end end

View File

@ -8,13 +8,18 @@ module Jasmine
def initialize(spec_files, runner) def initialize(spec_files, runner)
@spec_files = spec_files @spec_files = spec_files
@runner = runner @runner = runner
end
def start
guess_example_locations guess_example_locations
@runner.start @runner.start
load_suite_info load_suite_info
@spec_results = {} @spec_results = {}
end
def stop
@runner.stop
end end
def script_path def script_path
@ -46,21 +51,21 @@ module Jasmine
end end
def load_suite_info def load_suite_info
while !eval_js('jasmine.getEnv().currentRunner.finished') do while !eval_js('jsApiReporter.started') do
sleep 0.1 sleep 0.1
end end
@suites = eval_js('Object.toJSON(reportingBridge.suiteInfo)') @suites = eval_js('JSON.stringify(jsApiReporter.suites)')
end end
def results_for(spec_id) def results_for(spec_id)
spec_id = spec_id.to_s spec_id = spec_id.to_s
return @spec_results[spec_id] if @spec_results[spec_id] return @spec_results[spec_id] if @spec_results[spec_id]
@spec_results[spec_id] = eval_js("Object.toJSON(reportingBridge.specResults[#{spec_id}])") @spec_results[spec_id] = eval_js("JSON.stringify(jsApiReporter.results[#{spec_id}])")
while @spec_results[spec_id].nil? do while @spec_results[spec_id].nil? do
sleep 0.1 sleep 0.1
@spec_results[spec_id] = eval_js("Object.toJSON(reportingBridge.specResults[#{spec_id}])") @spec_results[spec_id] = eval_js("JSON.stringify(jsApiReporter.results[#{spec_id}])")
end end
@spec_results[spec_id] @spec_results[spec_id]
@ -83,7 +88,7 @@ module Jasmine
elsif type == "spec" elsif type == "spec"
me.declare_spec(self, suite_or_spec) me.declare_spec(self, suite_or_spec)
else else
raise "unknown type #{type}" raise "unknown type #{type} for #{suite_or_spec.inspect}"
end end
end end
end end
@ -102,9 +107,20 @@ module Jasmine
def report_spec(spec_id) def report_spec(spec_id)
spec_results = results_for(spec_id) spec_results = results_for(spec_id)
messages = spec_results['messages'].join "\n---\n" out = ""
puts messages messages = spec_results['messages'].each do |message|
fail messages if (spec_results['result'] != 'passed') out << message["message"]
out << "\n"
unless message["passed"]
stack_trace = message["trace"]["stack"]
out << stack_trace.gsub(/\(.*\)@http:\/\/localhost:[0-9]+\/specs\//, "/spec/")
out << "\n"
end
end
fail out unless spec_results['result'] == 'passed'
puts out
end end
private private

View File

@ -4,13 +4,16 @@
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type"/> <meta content="text/html;charset=UTF-8" http-equiv="Content-Type"/>
<title>Jasmine suite</title> <title>Jasmine suite</title>
<script src="/specs/javascripts/jasmine/lib/jasmine.js"></script> <script src="/specs/javascripts/jasmine/lib/jasmine.js"></script>
<script src="/specs/javascripts/jasmine/lib/json2.js"></script><!-- todo: don't load JSON in the test runner! [20090710 xw] -->
<script src="/specs/javascripts/jasmine/lib/TrivialReporter.js"></script> <script src="/specs/javascripts/jasmine/lib/TrivialReporter.js"></script>
<script src="/specs/javascripts/screw-jasmine-compat.js"></script> <script src="/specs/javascripts/screw-jasmine-compat.js"></script>
<script type="text/javascript"></script> <script type="text/javascript"></script>
<link href="/core/jasmine.css" rel="stylesheet"/> <link href="/core/jasmine.css" rel="stylesheet"/>
<script type="text/javascript"> <script type="text/javascript">
var jasmineEnv = jasmine.getEnv(); var jasmineEnv = jasmine.getEnv();
jasmineEnv.reporter = new jasmine.TrivialReporter(); var jsApiReporter = new jasmine.JsApiReporter();
jasmineEnv.addReporter(jsApiReporter);
jasmineEnv.addReporter(new jasmine.TrivialReporter());
window.onload = function() { window.onload = function() {
jasmineEnv.execute(); jasmineEnv.execute();
}; };

View File

@ -869,7 +869,65 @@ jasmine.Reporter.prototype.reportSpecResults = function(spec) {
}; };
//noinspection JSUnusedLocalSymbols //noinspection JSUnusedLocalSymbols
jasmine.Reporter.prototype.log = function (str) { jasmine.Reporter.prototype.log = function(str) {
};
/** JavaScript API reporter.
*
* @constructor
*/
jasmine.JsApiReporter = function() {
this.started = false;
this.finished = false;
this.suites = [];
this.results = {};
};
jasmine.JsApiReporter.prototype.reportRunnerStarting = function(runner) {
this.started = true;
for (var i = 0; i < runner.suites.length; i++) {
var suite = runner.suites[i];
this.suites.push(this.summarize_(suite));
}
};
jasmine.JsApiReporter.prototype.summarize_ = function(suiteOrSpec) {
var summary = {
id: suiteOrSpec.id,
name: suiteOrSpec.description,
type: suiteOrSpec instanceof jasmine.Suite ? 'suite' : 'spec',
children: []
};
if (suiteOrSpec.specs) {
for (var i = 0; i < suiteOrSpec.specs.length; i++) {
summary.children.push(this.summarize_(suiteOrSpec.specs[i]));
}
}
return summary;
};
//noinspection JSUnusedLocalSymbols
jasmine.JsApiReporter.prototype.reportRunnerResults = function(runner) {
this.finished = true;
};
//noinspection JSUnusedLocalSymbols
jasmine.JsApiReporter.prototype.reportSuiteResults = function(suite) {
};
//noinspection JSUnusedLocalSymbols
jasmine.JsApiReporter.prototype.reportSpecResults = function(spec) {
this.results[spec.id] = {
messages: spec.results.getItems(),
result: spec.results.failedCount > 0 ? "failed" : "passed"
};
};
//noinspection JSUnusedLocalSymbols
jasmine.JsApiReporter.prototype.log = function(str) {
}; };
jasmine.Matchers = function(env, actual, results) { jasmine.Matchers = function(env, actual, results) {

58
src/JsApiReporter.js Normal file
View File

@ -0,0 +1,58 @@
/** JavaScript API reporter.
*
* @constructor
*/
jasmine.JsApiReporter = function() {
this.started = false;
this.finished = false;
this.suites = [];
this.results = {};
};
jasmine.JsApiReporter.prototype.reportRunnerStarting = function(runner) {
this.started = true;
for (var i = 0; i < runner.suites.length; i++) {
var suite = runner.suites[i];
this.suites.push(this.summarize_(suite));
}
};
jasmine.JsApiReporter.prototype.summarize_ = function(suiteOrSpec) {
var summary = {
id: suiteOrSpec.id,
name: suiteOrSpec.description,
type: suiteOrSpec instanceof jasmine.Suite ? 'suite' : 'spec',
children: []
};
if (suiteOrSpec.specs) {
for (var i = 0; i < suiteOrSpec.specs.length; i++) {
summary.children.push(this.summarize_(suiteOrSpec.specs[i]));
}
}
return summary;
};
//noinspection JSUnusedLocalSymbols
jasmine.JsApiReporter.prototype.reportRunnerResults = function(runner) {
this.finished = true;
};
//noinspection JSUnusedLocalSymbols
jasmine.JsApiReporter.prototype.reportSuiteResults = function(suite) {
};
//noinspection JSUnusedLocalSymbols
jasmine.JsApiReporter.prototype.reportSpecResults = function(spec) {
this.results[spec.id] = {
messages: spec.results.getItems(),
result: spec.results.failedCount > 0 ? "failed" : "passed"
};
};
//noinspection JSUnusedLocalSymbols
jasmine.JsApiReporter.prototype.log = function(str) {
};

View File

@ -22,6 +22,6 @@ jasmine.Reporter.prototype.reportSpecResults = function(spec) {
}; };
//noinspection JSUnusedLocalSymbols //noinspection JSUnusedLocalSymbols
jasmine.Reporter.prototype.log = function (str) { jasmine.Reporter.prototype.log = function(str) {
}; };