-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnodeunit-adapter.js
120 lines (92 loc) · 3.28 KB
/
nodeunit-adapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// /vendor/testem/nodeunit-adapter.js
// david kaye
// 27 FEB 2014
// part of where.js custom strategy exercis
//
// modified from
// https://github.com/airportyh/testem/tree/master/examples/customAdapter.js
//================
// The interface for an adapter is simply a function that takes
// a [Socket.IO](http://socket.io/) socket. When test events
// `tests-start`, `test-result`, `all-test-results` come in,
// the adapter is responsible for sending these events to the socket.
//================
function nodeunitAdapter(socket) {
// nodeunit.run() is called from the browser suite to kick things off.
// nodeunit.run() calls nodeunit.runModules().
// We intercept runModules() here and populate the real options object with
// callbacks for each event we care about in the lifecyle.
var runModules = nodeunit.runModules;
nodeunit.runModules = function (modules, options) {
// restore control first
nodeunit.runModules = runModules;
// options could be nothing so add a no-op facade for the API events we'll
// intercept
function empty() {}
options || (options = {});
// hold on to the handlers for each lifecycle event which we'll intercept.
var moduleStart = options.moduleStart || empty;
var testDone = options.testDone || empty;
var done = options.done || empty;
var allResults = {
failed: 0,
passed: 0,
total: 0,
tests: []
};
// intercept for suite start
// this is a no-op in nodeunit - leave this here in case that changes
options.moduleStart = function (name) {
// tell nodeunit first
moduleStart.call(options, name);
};
// this is the heart of it. intercept calls to testDone() and populate test
// results with Testem-specific details.
options.testDone = function (name, assertions) {
// tell nodeunit first
testDone.call(options, name, assertions);
var testResult = {
name: name,
passed: 0,
failed: 0,
total: 1,
items: []
};
for (var i = 0, result, error, message; i < assertions.length; ++i) {
testResult.total++;
allResults.total++;
result = assertions[i];
error = result.error;
if (error) {
message = error.message || result.message;
testResult.failed++;
allResults.failed++;
testResult.items.push({
passed: false,
message: message,
stack: error.stack || undefined
});
} else {
testResult.passed++;
allResults.passed++;
}
}
allResults.tests.push(testResult);
// report result to Testem
socket.emit('test-result', testResult);
};
// intercept done for all tests
options.done = function (assertions) {
// tell nodeunit first
done.call(options, assertions);
// report all results to Testem
socket.emit('all-test-results', allResults);
};
// tell Testem we're under way
socket.emit('tests-start');
// tell nodeunit we're under way...
runModules(modules, options);
};
}
// Tell Testem to use your adapter
Testem.useCustomAdapter(nodeunitAdapter);