-
Notifications
You must be signed in to change notification settings - Fork 3
/
worker.js
175 lines (149 loc) · 4.65 KB
/
worker.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
// args
var fileName = process.argv[3];
var host = process.argv[4];
var port = process.argv[5];
var configPath = process.argv[6] !== 'null' ? process.argv[6] : null;
var reporter = process.argv[7];
var verbose = process.argv[8];
var browserName = process.argv[9];
var quit = process.argv[10] === 'true' ? true : false;
var Driver = require('./driver');
var Mocha = require('mocha');
var async = require('async');
var debug = require('debug')('worker');
var DEFAULT_BROWSER_QUIT_TIMEOUT = 60000;
var DEFAULT_BROWSER_START_TIMEOUT = 60000;
// testing
var wd = require('wd');
var isRetry = false;
var colors = require('colors');
// end testing
var ChildProcessSoftError = require('forq').Errors.ChildProcessSoftError;
var d = new Driver({
host: host,
port: port,
parentProcess: process,
configPath: configPath,
reporter: reporter,
verbose: verbose === 'true' ? true : false,
browserName: browserName ? browserName : null
});
var browser = d.browser;
var errors = [];
var config = {};
var defaultMochaOpts = {
reporter: 'spec'
};
try {
config = require(configPath);
} catch (e) {
debug('No config file found at "' + configPath + '". Using defaults.');
}
async.series([
function(done) {
// quit browser when tests are done
var browserStartTimer;
browserStartTimer = setTimeout(function(){
if (d.verbose) {
console.log(('warning: browser failed to start in '+DEFAULT_BROWSER_QUIT_TIMEOUT+'ms').red);
}
done();
// TODO: implement this
// done(new Error('Browser Start Error'));
}, DEFAULT_BROWSER_START_TIMEOUT);
d.on('ready', function(){
var mochaOpts = config.mocha || defaultMochaOpts;
var mocha = new Mocha(mochaOpts);
var started = false;
var startTimer;
var runner;
debug('preparing mocha for test file', fileName);
clearTimeout(browserStartTimer);
debug('browser has successfully started. starting mocha');
mocha.addFile(fileName);
mocha.suite
.on('pre-require', function(ctx, file) {
startTimer = setTimeout(function(){
if (!started) {
console.log(("mocha test "+fileName+" failed to start").red);
browser.quit();
done(new Error('mocha test failed to start'));
}
}, DEFAULT_BROWSER_START_TIMEOUT );
ctx.wd = wd;
ctx.browser = browser;
// prepare context
if (configPath) {
ctx.config = config;
// TODO: remove this
ctx.c = ctx.config.data;
ctx.s = ctx.config.steps;
ctx.CSS = ctx.config.selectors;
ctx.id = Date.now().toString('16').slice(2);
ctx.isGulp = true;
ctx.isRetry = isRetry;
}
});
runner = mocha.run(function(failures){
var error;
if (failures > 0 || errors.length > 0) {
debug('FAILED: '.red + fileName);
error = new Error('test failed!');
} else {
debug('PASSED: '.green + fileName);
error = null;
}
// quit browser when tests are done
var timer = setTimeout(function(){
console.log(('warning: browser failed to quit in '+DEFAULT_BROWSER_QUIT_TIMEOUT+'ms').red);
done();
// TODO: implement a specific error type for this
// done(new Error('Browser Quit Error'));
}, DEFAULT_BROWSER_QUIT_TIMEOUT);
if (!quit) {
clearTimeout(timer);
debug('browser has successfully quit. closing worker');
done(error);
} else {
browser.quit().then(function(){
// kill the above timer
clearTimeout(timer);
debug('browser has successfully quit. closing worker');
done(error);
});
}
});
runner.on('suite', function(){
clearTimeout(startTimer);
started = true;
debug("suite started");
});
runner.on('fail', function(ctx, err){
if (err) {
new ChildProcessSoftError(err);
errors.push(err);
}
});
});
d.on('error', function (err) {
if (err) {
// handle browser startup errors
if (err.name === 'Browser Start Error') {
var e = new ChildProcessSoftError(err);
errors.push(e);
} else {
// go easy on more generic, non-fatal errors
console.log("Driver Encountered an error: ", err);
}
}
});
}
], function (err) {
if (err) {
var e = new ChildProcessSoftError(err);
errors.push(e);
debug('an error occured before exiting fork');
} else {
debug('worker callback fired without firing an error');
}
});