-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathchild.js
More file actions
181 lines (153 loc) · 4.24 KB
/
child.js
File metadata and controls
181 lines (153 loc) · 4.24 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var QUnit = require('qunitjs'),
path = require('path'),
_ = require('underscore'),
trace = require('tracejs').trace,
coverage = require('./coverage'),
generators = require('./generators'),
co = require('co');
// cycle.js: This file contains two functions, JSON.decycle and JSON.retrocycle,
// which make it possible to encode cyclical structures and dags in JSON, and to
// then recover them. JSONPath is used to represent the links.
// http://GOESSNER.net/articles/JsonPath/
require('../support/json/cycle');
var options = JSON.parse(process.argv.pop()),
currentModule = path.basename(options.code.path, '.js'),
currentTest;
if(!options.debug) {
// send ping messages to when child is blocked.
// after I sent the first ping, testrunner will start to except the next ping
// within maxBlockDuration, otherwise this process will be killed
process.send({event: 'ping'});
setInterval(function () {
process.send({event: 'ping'});
}, Math.floor(options.maxBlockDuration / 2));
}
process.on('uncaughtException', function(err) {
if (QUnit.config.current) {
QUnit.ok(false, 'Test threw unexpected exception: ' + err.message);
QUnit.start();
}
process.send({
event: 'uncaughtException',
data: {
message: err.message,
stack: err.stack
}
});
});
QUnit.config.autorun = false;
QUnit.config.autostart = false;
// make qunit api global, like it is in the browser
_.extend(global, QUnit);
// as well as the QUnit variable itself
global.QUnit = QUnit;
/**
* Require a resource.
* @param {Object} res
*/
function _require(res, addToGlobal) {
var exports = require(res.path.replace(/\.js$/, ''));
if (addToGlobal) {
// resource can define 'namespace' to expose its exports as a named object
if (res.namespace) {
global[res.namespace] = exports;
} else {
_.extend(global, exports);
}
}
QUnit.start();
}
/**
* Callback for each started test.
* @param {Object} test
*/
QUnit.testStart(function(test) {
// currentTest is undefined while first test is not done yet
currentTest = test.name;
// use last module name if no module name defined
currentModule = test.module || currentModule;
});
/**
* Callback for each assertion.
* @param {Object} data
*/
QUnit.log(function(data) {
data.test = this.config.current.testName;
data.module = currentModule;
process.send({
event: 'assertionDone',
data: JSON.decycle(data)
});
});
/**
* Callback for one done test.
* @param {Object} test
*/
QUnit.testDone(function(data) {
// use last module name if no module name defined
data.module = data.module || currentModule;
process.send({
event: 'testDone',
data: data
});
});
/**
* Callback for all done tests in the file.
* @param {Object} res
*/
QUnit.done(_.debounce(function(data) {
data.coverage = global.__coverage__;
process.send({
event: 'done',
data: data
});
}, 1000));
if (generators.support) {
var test = QUnit.test;
/**
* Support generators.
*/
global.test = QUnit.test = function(testName, expected, callback, async) {
var fn;
if (arguments.length === 2) {
callback = expected;
expected = null;
}
if (generators.isGeneratorFn(callback)) {
fn = function(assert) {
stop();
co(callback).call(this, assert, function(err) {
if (err) return console.log(err.stack)
start();
});
};
} else {
fn = callback;
}
return test.call(this, testName, expected, fn, async);
};
}
/**
* Provide better stack traces
*/
var error = console.error;
console.error = function(obj) {
// log full stacktrace
if (obj && obj.stack) {
obj = trace(obj);
}
return error.apply(this, arguments);
};
if (options.coverage) {
coverage.instrument(options);
}
// require deps
options.deps.forEach(function(dep) {
_require(dep, true);
});
// require code
_require(options.code, true);
// require tests
options.tests.forEach(function(test) {
_require(test, false);
});