UNPKG

4.43 kBJavaScriptView Raw
1/*!
2 * Nodeunit
3 * Copyright (c) 2010 Caolan McMahon
4 * MIT Licensed
5 *
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
10 */
11
12
13/**
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.
16 */
17
18
19/**
20 * Reporter info string
21 */
22
23exports.info = "Browser-based test reporter";
24
25
26/**
27 * Run all tests within each module, reporting the results
28 *
29 * @param {Array} files
30 * @api public
31 */
32
33exports.run = function (modules, options, callback) {
34 var start = new Date().getTime(), div, textareas, displayErrorsByDefault;
35 options = options || {};
36 div = options.div || document.body;
37 textareas = options.textareas;
38 displayErrorsByDefault = options.displayErrorsByDefault;
39
40 function setText(el, txt) {
41 if ('innerText' in el) {
42 el.innerText = txt;
43 }
44 else if ('textContent' in el){
45 el.textContent = txt;
46 }
47 }
48
49 function getOrCreate(tag, id) {
50 var el = document.getElementById(id);
51 if (!el) {
52 el = document.createElement(tag);
53 el.id = id;
54 div.appendChild(el);
55 }
56 return el;
57 };
58
59 var header = getOrCreate('h1', 'nodeunit-header');
60 var banner = getOrCreate('h2', 'nodeunit-banner');
61 var userAgent = getOrCreate('h2', 'nodeunit-userAgent');
62 var tests = getOrCreate('ol', 'nodeunit-tests');
63 var result = getOrCreate('p', 'nodeunit-testresult');
64
65 setText(userAgent, navigator.userAgent);
66
67 nodeunit.runModules(modules, {
68 moduleStart: function (name) {
69 /*var mheading = document.createElement('h2');
70 mheading.innerText = name;
71 results.appendChild(mheading);
72 module = document.createElement('ol');
73 results.appendChild(module);*/
74 },
75 testDone: function (name, assertions) {
76 var test = document.createElement('li');
77 var strong = document.createElement('strong');
78 strong.innerHTML = name + ' <b style="color: black;">(' +
79 '<b class="fail">' + assertions.failures() + '</b>, ' +
80 '<b class="pass">' + assertions.passes() + '</b>, ' +
81 assertions.length +
82 ')</b>';
83 test.className = assertions.failures() ? 'fail': 'pass';
84 test.appendChild(strong);
85
86 var aList = document.createElement('ol');
87 aList.style.display = displayErrorsByDefault ? 'block' : 'none';
88 (displayErrorsByDefault ? strong : test).onclick = function () {
89 var d = aList.style.display;
90 aList.style.display = (d == 'none') ? 'block': 'none';
91 };
92 for (var i=0; i<assertions.length; i++) {
93 var li = document.createElement('li');
94 var a = assertions[i];
95 if (a.failed()) {
96 li.innerHTML = (a.message || a.method || 'no message') +
97 (textareas ?
98 '<textarea rows="20" cols="100">' + (a.error.stack || a.error) + '</textarea>' :
99 '<pre>' + (a.error.stack || a.error) + '</pre>');
100 li.className = 'fail';
101 }
102 else {
103 li.innerHTML = a.message || a.method || 'no message';
104 li.className = 'pass';
105 }
106 aList.appendChild(li);
107 }
108 test.appendChild(aList);
109 tests.appendChild(test);
110 },
111 done: function (assertions) {
112 var end = new Date().getTime();
113 var duration = end - start;
114
115 var failures = assertions.failures();
116 banner.className = failures ? 'fail': 'pass';
117
118 result.innerHTML = 'Tests completed in ' + duration +
119 ' milliseconds.<br/><span class="passed">' +
120 assertions.passes() + '</span> assertions of ' +
121 '<span class="all">' + assertions.length + '<span> passed, ' +
122 assertions.failures() + ' failed.';
123
124 if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
125 }
126 });
127};