3 * Copyright (c) 2010 Caolan McMahon
6 * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
7 * You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
8 * Only code on that line will be removed, its mostly to avoid requiring code
9 * that is node specific
14 * NOTE: this test runner is not listed in index.js because it cannot be
15 * used with the command-line tool, only inside the browser.
20 * Reporter info string
23 exports.info = "Browser-based test reporter";
27 * Run all tests within each module, reporting the results
29 * @param {Array} files
33 exports.run = function (modules, options) {
34 var start = new Date().getTime(), div;
35 options = options || {};
36 div = options.div || document.body;
38 function setText(el, txt) {
39 if ('innerText' in el) {
42 else if ('textContent' in el){
47 function getOrCreate(tag, id) {
48 var el = document.getElementById(id);
50 el = document.createElement(tag);
57 var header = getOrCreate('h1', 'nodeunit-header');
58 var banner = getOrCreate('h2', 'nodeunit-banner');
59 var userAgent = getOrCreate('h2', 'nodeunit-userAgent');
60 var tests = getOrCreate('ol', 'nodeunit-tests');
61 var result = getOrCreate('p', 'nodeunit-testresult');
63 setText(userAgent, navigator.userAgent);
65 nodeunit.runModules(modules, {
66 moduleStart: function (name) {
67 /*var mheading = document.createElement('h2');
68 mheading.innerText = name;
69 results.appendChild(mheading);
70 module = document.createElement('ol');
71 results.appendChild(module);*/
73 testDone: function (name, assertions) {
74 var test = document.createElement('li');
75 var strong = document.createElement('strong');
76 strong.innerHTML = name + ' <b style="color: black;">(' +
77 '<b class="fail">' + assertions.failures() + '</b>, ' +
78 '<b class="pass">' + assertions.passes() + '</b>, ' +
81 test.className = assertions.failures() ? 'fail': 'pass';
82 test.appendChild(strong);
84 var aList = document.createElement('ol');
85 aList.style.display = 'none';
86 test.onclick = function () {
87 var d = aList.style.display;
88 aList.style.display = (d == 'none') ? 'block': 'none';
90 for (var i=0; i<assertions.length; i++) {
91 var li = document.createElement('li');
92 var a = assertions[i];
94 li.innerHTML = (a.message || a.method || 'no message') +
95 '<pre>' + (a.error.stack || a.error) + '</pre>';
96 li.className = 'fail';
99 li.innerHTML = a.message || a.method || 'no message';
100 li.className = 'pass';
102 aList.appendChild(li);
104 test.appendChild(aList);
105 tests.appendChild(test);
107 done: function (assertions) {
108 var end = new Date().getTime();
109 var duration = end - start;
111 var failures = assertions.failures();
112 banner.className = failures ? 'fail': 'pass';
114 result.innerHTML = 'Tests completed in ' + duration +
115 ' milliseconds.<br/><span class="passed">' +
116 assertions.passes() + '</span> assertions of ' +
117 '<span class="all">' + assertions.length + '<span> passed, ' +
118 assertions.failures() + ' failed.';