Skip to content

Commit c95aeb3

Browse files
QUnit plugins: fixed callbacks addition, local variables are moved into closure. Tests can be run on IE8-.
1 parent 8b9324b commit c95aeb3

2 files changed

Lines changed: 102 additions & 102 deletions

File tree

test/lib/qunit-browserscope.js

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
1+
/*global QUnit */
2+
// global variable - test results for BrowserScope
13
var _bTestResults = {};
4+
(function() {
5+
var testKey = 'agt1YS1wcm9maWxlcnINCxIEVGVzdBjr68MRDA';
6+
var callbackName = "showBrowserScopeResults";
27

3-
// Add URL option in QUnit to toggle publishing results to BrowserScope.org
4-
QUnit.config.urlConfig.push("publish");
5-
QUnit.config.testTimeout = 1000; // Timeout for async tests
8+
// Add URL option in QUnit to toggle publishing results to BrowserScope.org
9+
QUnit.config.urlConfig.push("publish");
10+
QUnit.config.testTimeout = 1000; // Timeout for async tests
611

7-
// Prevent overwriting other hooks
8-
if (typeof QUnit.testDone === 'function') {
9-
QUnit.oldTestDone = QUnit.testDone;
10-
}
11-
if (typeof QUnit.done === 'function') {
12-
QUnit.oldDone = QUnit.done;
13-
}
12+
// Build-up the test results beacon for BrowserScope.org
13+
QUnit.testDone(function(test) {
14+
// make sure all assertions passed successfully
15+
if (!test.failed && test.total === test.passed) {
16+
_bTestResults[test.name] = 1;
17+
} else {
18+
_bTestResults[test.name] = 0;
19+
}
20+
});
1421

15-
// Build-up the test results beacon for BrowserScope.org
16-
QUnit.testDone = function(test) {
17-
QUnit.oldTestDone && QUnit.oldTestDone(test);
18-
// make sure all assertions passed successfully
19-
if (!test.failed && test.total === test.passed) {
20-
_bTestResults[test.name] = 1;
21-
} else {
22-
_bTestResults[test.name] = 0;
23-
}
24-
}
22+
// If the user agreed to publish results to BrowserScope.org, go for it!
23+
QUnit.done(function(result) {
24+
if (QUnit.config.publish) {
25+
var newScript = document.createElement('script');
26+
newScript.src = 'http://www.browserscope.org/user/beacon/' + testKey + "?callback=" + callbackName;
27+
var firstScript = document.getElementsByTagName('script')[0];
28+
firstScript.parentNode.insertBefore(newScript, firstScript);
29+
}
30+
});
2531

26-
// If the user agreed to publish results to BrowserScope.org, go for it!
27-
QUnit.done = function(result) {
28-
QUnit.oldDone && QUnit.oldDone(result);
29-
if (QUnit.config.publish) {
30-
var testKey = 'agt1YS1wcm9maWxlcnINCxIEVGVzdBjr68MRDA';
31-
var newScript = document.createElement('script');
32-
var firstScript = document.getElementsByTagName('script')[0];
33-
34-
newScript.src = 'http://www.browserscope.org/user/beacon/' + testKey + "?callback=showResults";
35-
firstScript.parentNode.insertBefore(newScript, firstScript);
36-
}
37-
}
38-
39-
// Load the results widget from browserscope.org
40-
function showResults() {
41-
var script = document.createElement('script');
42-
script.src = "http://www.browserscope.org/user/tests/table/agt1YS1wcm9maWxlcnINCxIEVGVzdBjr68MRDA?o=js";
43-
document.body.appendChild(script);
44-
}
32+
// Load the results widget from browserscope.org
33+
window[callbackName] = function() {
34+
var script = document.createElement('script');
35+
script.src = "http://www.browserscope.org/user/tests/table/" + testKey + "?o=js";
36+
document.body.appendChild(script);
37+
};
38+
}());

test/lib/qunit-junit-outputter.js

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,76 @@
1-
var module, moduleStart, testStart, testCases = [],
2-
current_test_assertions = [];
3-
console.log('<?xml version="1.0" encoding="UTF-8"?>');
4-
console.log('<testsuites name="testsuites">');
5-
QUnit.begin = function() {
6-
// That does not work when invoked in PhantomJS
7-
}
1+
/*global QUnit, console */
2+
(function() {
3+
var module, moduleStart, testStart, testCases = [], current_test_assertions = [];
4+
console.log('<?xml version="1.0" encoding="UTF-8"?>');
5+
console.log('<testsuites name="testsuites">');
86

9-
QUnit.moduleStart = function(context) {
10-
moduleStart = new Date();
11-
module = context.name;
12-
testCases = [];
13-
}
7+
QUnit.begin(function() {
8+
// That does not work when invoked in PhantomJS
9+
});
1410

15-
QUnit.moduleDone = function(context) {
16-
// context = { name, failed, passed, total }
17-
var xml = '\t<testsuite name="' + context.name + '" errors="0" failures="' + context.failed + '" tests="' + context.total + '" time="' + (new Date() - moduleStart) / 1000 + '"';
18-
if (testCases.length) {
19-
xml += '>\n';
20-
for (var i = 0, l = testCases.length; i < l; i++) {
21-
xml += testCases[i];
22-
}
23-
xml += '\t</testsuite>';
24-
} else {
25-
xml += '/>\n';
26-
}
27-
console.log(xml);
28-
}
11+
QUnit.moduleStart(function(context) {
12+
// context = { name }
13+
moduleStart = new Date();
14+
module = context.name;
15+
testCases = [];
16+
});
2917

30-
QUnit.testStart = function() {
31-
testStart = new Date();
32-
}
18+
QUnit.moduleDone(function(context) {
19+
// context = { name, failed, passed, total }
20+
var xml = '\t<testsuite name="' + context.name + '" errors="0" failures="' + context.failed + '" tests="' + context.total + '" time="' + (new Date() - moduleStart) / 1000 + '"';
21+
if (testCases.length) {
22+
xml += '>\n';
23+
for (var i = 0, l = testCases.length; i < l; i++) {
24+
xml += testCases[i];
25+
}
26+
xml += '\t</testsuite>';
27+
} else {
28+
xml += '/>\n';
29+
}
30+
console.log(xml);
31+
});
3332

34-
QUnit.testDone = function(result) {
35-
// result = { name, failed, passed, total }
36-
var xml = '\t\t<testcase classname="' + module + '" name="' + result.name + '" time="' + (new Date() - testStart) / 1000 + '"';
37-
if (result.failed) {
38-
xml += '>\n';
39-
for (var i = 0; i < current_test_assertions.length; i++) {
40-
xml += "\t\t\t" + current_test_assertions[i];
41-
}
42-
xml += '\t\t</testcase>\n';
43-
} else {
44-
xml += '/>\n';
45-
}
46-
current_test_assertions = [];
47-
testCases.push(xml);
48-
};
33+
QUnit.testStart(function() {
34+
// context = { name }
35+
testStart = new Date();
36+
});
4937

50-
QUnit.log = function(details) {
51-
//details = { result , actual, expected, message }
52-
if (details.result) {
53-
return;
54-
}
55-
var message = details.message || "";
56-
if (details.expected) {
57-
if (message) {
58-
message += ", ";
59-
}
60-
message = "expected: " + details.expected + ", but was: " + details.actual;
61-
}
62-
var xml = '<failure type="failed" message="' + message + '"/>\n'
38+
QUnit.testDone(function(result) {
39+
// result = { name, failed, passed, total }
40+
var xml = '\t\t<testcase classname="' + module + '" name="' + result.name + '" time="' + (new Date() - testStart) / 1000 + '"';
41+
if (result.failed) {
42+
xml += '>\n';
43+
for (var i = 0; i < current_test_assertions.length; i++) {
44+
xml += "\t\t\t" + current_test_assertions[i];
45+
}
46+
xml += '\t\t</testcase>\n';
47+
} else {
48+
xml += '/>\n';
49+
}
50+
current_test_assertions = [];
51+
testCases.push(xml);
52+
});
6353

64-
current_test_assertions.push(xml);
65-
};
54+
QUnit.log(function(details) {
55+
// details = { result, actual, expected, message }
56+
if (details.result) {
57+
return;
58+
}
59+
var message = details.message || "";
60+
if (details.expected) {
61+
if (message) {
62+
message += ", ";
63+
}
64+
message += "expected: " + details.expected + ", but was: " + details.actual;
65+
}
66+
var xml = '<failure type="failed" message="' + message + '"/>\n';
6667

67-
QUnit.done = function(result) {
68-
console.log('</testsuites>');
69-
return result.failed > 0 ? 1 : 0;
70-
};
68+
current_test_assertions.push(xml);
69+
});
70+
71+
QUnit.done(function(result) {
72+
// result = { failed, passed, total, runtime }
73+
console.log('</testsuites>');
74+
return result.failed > 0 ? 1 : 0;
75+
});
76+
}());

0 commit comments

Comments
 (0)