/
server.js
235 lines (205 loc) · 7.15 KB
/
server.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
TEST_FRAMEWORK_NAME = "mocha";
if (!process.env.NODE_ENV === "development"){
console.log("process.env.NODE ENV != DEVELOPMENT, TESTS WILL NOT BE RAN");
}
else {
if (Velocity && Velocity.registerTestingFramework){
Velocity.registerTestingFramework(TEST_FRAMEWORK_NAME, {
regex: 'mocha/.+\\.(js|coffee|litcoffee|coffee\\.md)$',
sampleTestGenerator: function(){
return [
{ path: "mocha/client/sampleClientTest.js",
contents: Assets.getText("sample-tests/client.js")
},
{ path: "mocha/server/sampleServerTest.js",
contents: Assets.getText("sample-tests/server.js")}
];
}
});
}
var clientTestsComplete = false;
var serverTestsComplete = false;
var Mocha = Npm.require("mocha");
var Fiber = Npm.require("fibers");
var childProcess = Npm.require('child_process');
var path = Npm.require('path');
var mkdirp = Npm.require("mkdirp");
ddpParentConnection = null;
var parentUrl = null;
var childUrl = null;
Meteor.startup(function(){
if (process.env.IS_MIRROR) {
console.log("MOCHA-WEB MIRROR LISTENING AT", process.env.ROOT_URL);
parentUrl = process.env.PARENT_URL;
console.log("PARENT URL", process.env.PARENT_URL);
ddpParentConnection = DDP.connect(parentUrl);
console.log("Running mocha server tests");
Meteor.call("velocity/reports/reset", function(err, result){
mocha.run(function(err){
serverTestsComplete = true;
if (clientTestsComplete){
markTestsComplete();
}
});
});
} else {
Meteor.call("velocity/mirrors/request", {
framework: 'mocha',
rootUrlPath: "?mocha=true"
}, function(err, msg){
if (err){
console.log("error requesting mirror", err);
} else {
var fileCopier = new Velocity.FileCopier({
targetFramework: "mocha",
shouldCopy: function (filepath) {
return true;
},
convertTestPathToMirrorPath: function (filePath) {
return filePath;
}
})
fileCopier.start();
}
});
}
});
function markTestsComplete(){
ddpParentConnection.call("velocity/reports/completed", {framework: "mocha"}, function(err){
if (err){
console.error("error calling testsComplete function", err);
}
});
}
Meteor.methods({
"mirrorInfo": function(){
return {
isMirror: process.env.IS_MIRROR,
parentUrl: process.env.PARENT_URL
}
},
"clientTestsComplete": function(){
// console.log("CLIENT TESTS COMPLETE");
clientTestsComplete = true;
if (serverTestsComplete){
markTestsComplete();
}
}
})
//if not a mirror don't do anything
MochaWeb.testOnly = function(callback){
// console.log("NO OP", mirror.isMirror);
};
setupMocha();
function setupMocha(){
if (! process.env.IS_MIRROR)
return;
// console.log("Enabling MochaWeb.testOnly");
//only when mocha has been explicity enabled (in a mirror)
//do we run the tests
MochaWeb.testOnly = function(callback){
callback();
}
global.chai = Npm.require("chai");
// enable stack trace with line numbers with assertions
global.chai.Assertion.includeStack = true;
global.mocha = new Mocha({ui: "bdd", reporter: MochaWeb.MeteorCollectionTestReporter});
console.log("SETUP GLOBALS");
setupGlobals();
}
function setupGlobals(){
//basically a direct copy from meteor/packages/meteor/dynamics_nodejs.js
//except the wrapped function has an argument (mocha distinguishes
//asynchronous tests from synchronous ones by the "length" of the
//function passed into it, before, etc.)
var moddedBindEnvironment = function (func, onException, _this) {
if (!Fiber.current)
throw new Error(noFiberMessage);
var boundValues = _.clone(Fiber.current._meteor_dynamics || []);
if (!onException || typeof(onException) === 'string') {
var description = onException || "callback of async function";
onException = function (error) {
Meteor._debug(
"Exception in " + description + ":",
error && error.stack || error
);
};
}
//note the callback variable present here
return function (callback) {
var args = _.toArray(arguments);
var runWithEnvironment = function () {
var savedValues = Fiber.current._meteor_dynamics;
try {
// Need to clone boundValues in case two fibers invoke this
// function at the same time
Fiber.current._meteor_dynamics = _.clone(boundValues);
var ret = func.apply(_this, args);
} catch (e) {
onException(e);
} finally {
Fiber.current._meteor_dynamics = savedValues;
}
return ret;
};
if (Fiber.current)
return runWithEnvironment();
Fiber(runWithEnvironment).run();
};
};
var mochaExports = {};
mocha.suite.emit("pre-require", mochaExports);
//console.log(mochaExports);
//patch up describe function so it plays nice w/ fibers
global.describe = function (name, func){
mochaExports.describe(name, Meteor.bindEnvironment(func, function(err){throw err; }));
};
global.describe.skip = mochaExports.describe.skip;
global.describe.only = mochaExports.describe.only;
//In Meteor, these blocks will all be invoking Meteor code and must
//run within a fiber. We must therefore wrap each with something like
//bindEnvironment. The function passed off to mocha must have length
//greater than zero if we want mocha to run it asynchronously. That's
//why it uses the moddedBindEnivronment function described above instead
//We're actually having mocha run all tests asynchronously. This
//is because mocha cannot tell when a synchronous fiber test has
//finished, because the test runner runs outside a fiber.
//It is possible that the mocha test runner could be run from within a
//fiber, but it was unclear to me how that could be done without
//forking mocha itself.
global['it'] = function (name, func){
wrappedFunc = function(callback){
if (func.length == 0){
func();
callback();
}
else {
func(callback);
}
}
boundWrappedFunction = moddedBindEnvironment(wrappedFunc, function(err){
throw err;
});
mochaExports['it'](name, boundWrappedFunction);
};
global.it.skip = mochaExports.it.skip;
global.it.only = mochaExports.it.only;
["before", "beforeEach", "after", "afterEach"].forEach(function(testFunctionName){
global[testFunctionName] = function (func){
wrappedFunc = function(callback){
if (func.length == 0){
func();
callback();
}
else {
func(callback);
}
}
boundWrappedFunction = moddedBindEnvironment(wrappedFunc, function(err){
throw err;
});
mochaExports[testFunctionName](boundWrappedFunction);
}
});
}
}