Skip to content

Commit 32d3b15

Browse files
committed
Switch to phantom-jasmine runner
1 parent 1d03679 commit 32d3b15

File tree

5 files changed

+160
-120
lines changed

5 files changed

+160
-120
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ bootstrap-tests:
6565
test: test-js test-python
6666

6767
test-js:
68-
phantomjs runtests.js tests/js/index.html
68+
phantomjs runtests.coffee tests/js/index.html
6969

7070
test-python:
7171
cd src && flake8 --exclude=migrations --ignore=E501,E225,E121,E123,E124,E125,E127,E128 --exit-zero sentry || exit 1

runtests.coffee

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/local/bin/phantomjs
2+
3+
# Runs a Jasmine Suite from an html page
4+
# @page is a PhantomJs page object
5+
# @exit_func is the function to call in order to exit the script
6+
7+
class PhantomJasmineRunner
8+
constructor: (@page, @exit_func = phantom.exit) ->
9+
@tries = 0
10+
@max_tries = 10
11+
12+
get_status: -> @page.evaluate(-> console_reporter.status)
13+
14+
terminate: ->
15+
switch @get_status()
16+
when "success" then @exit_func 0
17+
when "fail" then @exit_func 1
18+
else @exit_func 2
19+
20+
# Script Begin
21+
if phantom.args.length == 0
22+
console.log "Need a url as the argument"
23+
phantom.exit 1
24+
25+
page = new WebPage()
26+
27+
runner = new PhantomJasmineRunner(page)
28+
29+
# Don't supress console output
30+
page.onConsoleMessage = (msg) ->
31+
console.log msg
32+
33+
# Terminate when the reporter singals that testing is over.
34+
# We cannot use a callback function for this (because page.evaluate is sandboxed),
35+
# so we have to *observe* the website.
36+
if msg == "ConsoleReporter finished"
37+
runner.terminate()
38+
39+
address = phantom.args[0]
40+
41+
page.open address, (status) ->
42+
if status != "success"
43+
console.log "can't load the address!"
44+
phantom.exit 1
45+
46+
# Now we wait until onConsoleMessage reads the termination signal from the log.

runtests.js

Lines changed: 0 additions & 90 deletions
This file was deleted.

tests/js/index.html

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<script type="text/javascript" src="lib/jasmine-1.2.0/jasmine.js"></script>
1010
<script type="text/javascript" src="lib/jasmine-1.2.0/jasmine-html.js"></script>
1111
<script type="text/javascript" src="lib/sinon-1.5.0.js"></script>
12+
<script type="text/javascript" src="lib/console-runner.js"></script>
1213

1314
<!-- include source files here... -->
1415
<script type="text/javascript" src="../../src/sentry/static/sentry/scripts/jquery.js"></script>
@@ -29,38 +30,18 @@
2930
<script type="text/javascript" src="spec/floatFormat.js"></script>
3031
<script type="text/javascript" src="spec/formatNumber.js"></script>
3132

32-
<script type="text/javascript">
33-
$(function() {
34-
var jasmineEnv = jasmine.getEnv();
35-
jasmineEnv.updateInterval = 1000;
36-
37-
var htmlReporter = new jasmine.HtmlReporter();
38-
39-
jasmineEnv.addReporter(htmlReporter);
40-
41-
jasmineEnv.specFilter = function(spec) {
42-
return htmlReporter.specFilter(spec);
43-
};
44-
45-
var currentWindowOnload = window.onload;
46-
47-
window.onload = function() {
48-
if (currentWindowOnload) {
49-
currentWindowOnload();
50-
}
51-
execJasmine();
52-
};
53-
54-
function execJasmine() {
55-
jasmineEnv.execute();
56-
}
57-
58-
});
59-
</script>
60-
6133
</head>
6234

6335
<body>
6436
<div id="dummy" style="display:none"></div>
37+
38+
<script type="text/javascript">
39+
var console_reporter = new jasmine.ConsoleReporter()
40+
$(function(){
41+
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
42+
jasmine.getEnv().addReporter(console_reporter);
43+
jasmine.getEnv().execute();
44+
});
45+
</script>
6546
</body>
6647
</html>

tests/js/lib/console-runner.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)