-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathexam.js
More file actions
443 lines (392 loc) · 14 KB
/
exam.js
File metadata and controls
443 lines (392 loc) · 14 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
'use strict';
const { addToQuery } = require('../utils/query-helper');
// npmlog is used to write to testem server logs and `--testem-debug` enables to save the log file.
const log = require('npmlog');
const {
combineOptionValueIntoArray,
getBrowserId,
getMultipleTestPages
} = require('../utils/test-page-helper');
const TestemEvents = require('../utils/testem-events');
const TestCommand = require('ember-cli/lib/commands/test'); // eslint-disable-line node/no-unpublished-require
const TestServerTask = require('./task/test-server');
const TestTask = require('./task/test');
module.exports = TestCommand.extend({
name: 'exam',
description: `Runs your app's test suite with more options than 'test'.`,
works: 'insideProject',
availableOptions: [
{
name: 'split',
type: Number,
description: 'A number of files to split your tests across.'
},
{
name: 'partition',
type: [Array, Number, String],
description: 'The number of the partition(s) to run after splitting.'
},
{
name: 'parallel',
type: [Number, String],
description: 'Runs your split tests on parallel child processes.'
},
{
name: 'load-balance',
type: Boolean,
default: false,
description:
'Load balance test modules. Test modules will be sorted by weight from slowest (acceptance) to fastest (eslint).'
},
{
name: 'random',
type: String,
default: false,
description:
'Randomizes your modules and tests while running your test suite.'
},
{
name: 'module-path',
type: [String],
aliases: ['mp'],
description: 'Filters the list of modules to only those that matches by module paths, the value accepts either string or regex.'
},
{
name: 'file-path',
type: [String],
aliases: ['fp'],
description: 'Filters the list of modules to only those that matches by test file paths, the value accepts either string or regex.'
},
{
name: 'replay-execution',
type: String,
default: false,
aliases: ['re'],
description:
'A JSON file path which maps from browser id(s) to a list of modules'
},
{
name: 'replay-browser',
type: [Array, Number, String],
aliases: ['rb'],
description: 'The browser id(s) to replay from the replay-execution file'
},
{
name: 'write-execution-file',
type: Boolean,
default: false,
aliases: ['wef'],
description:
'Allows writing a test-execution json file after running your test suite'
},
{
name: 'write-module-metadata-file',
type: Boolean,
default: false,
aliases: ['wmmf'],
description:
'Allows writing a module metadata json file after running your test suite'
}
].concat(TestCommand.prototype.availableOptions),
init() {
this._super(...arguments);
this.tasks.Test = TestTask;
this.tasks.TestServer = TestServerTask;
this.testemEvents = new TestemEvents(this.project.root);
const pkg = this.project.pkg;
const dependencies = pkg.dependencies || {};
const devDependencies = pkg.devDependencies || {};
this.emberCliVersion = devDependencies['ember-cli'] || dependencies['ember-cli'];
},
/**
* Validates commandOptions
*
* @private
* @param {Object} commandOptions
* @return {Object} A map of what switches are enabled
*/
_validateOptions(commandOptions) {
const Validator = require('../utils/tests-options-validator');
const validator = new Validator(
commandOptions,
this._getTestFramework(),
this.emberCliVersion
);
return validator.validateCommands();
},
/**
* Gets the name of the test framework being used by the project.
*
* @private
* @return {string}
*/
_getTestFramework() {
const pkg = this.project.pkg;
const dependencies = pkg.dependencies || {};
const devDependencies = pkg.devDependencies || {};
if (dependencies['ember-mocha'] || devDependencies['ember-mocha']) {
return 'mocha';
} else {
return 'qunit';
}
},
/**
* Validates the command options and then runs the original test command.
*
* @param {Object} commandOptions
* @override
*/
run(commandOptions) {
this.commands = this._validateOptions(commandOptions);
// TODO: explore not mutating the commandOptions input
if (commandOptions.split) {
commandOptions.query = addToQuery(
commandOptions.query,
'split',
commandOptions.split
);
process.env.EMBER_EXAM_SPLIT_COUNT = commandOptions.split;
// Ignore the partition option when paralleling (we'll fill it in later)
if (!commandOptions.parallel && commandOptions.partition) {
const partitions = combineOptionValueIntoArray(
commandOptions.partition
);
for (let i = 0; i < partitions.length; i++) {
commandOptions.query = addToQuery(
commandOptions.query,
'partition',
partitions[i]
);
}
}
}
if (commandOptions.modulePath) {
commandOptions.query = addToQuery(
commandOptions.query,
'modulePath',
commandOptions.modulePath
);
}
if (commandOptions.filePath) {
commandOptions.query = addToQuery(
commandOptions.query,
'filePath',
commandOptions.filePath
);
}
if (commandOptions.loadBalance) {
commandOptions.query = addToQuery(
commandOptions.query,
'loadBalance',
commandOptions.loadBalance
);
}
if (commandOptions.replayBrowser) {
commandOptions.replayBrowser = combineOptionValueIntoArray(
commandOptions.replayBrowser
);
}
if (typeof commandOptions.random !== 'undefined') {
commandOptions.query = this._randomize(
commandOptions.random,
commandOptions.query
);
}
return this._super.run.apply(this, arguments);
},
/**
* Adds a `seed` param to the `query` to support randomization. Currently
* only works with QUnit.
*
* @param {string} random
* @param {string} query
* @return {string}
*/
_randomize(random, query) {
const seed =
random !== ''
? random
: Math.random()
.toString(36)
.slice(2);
this.ui.writeLine('Randomizing tests with seed: ' + seed);
return addToQuery(query, 'seed', seed);
},
/**
* Customizes the Testem config to have multiple test pages if attempting to
* run in parallel or load-balance. It is important that the user specifies
* the number of launchers to run in parallel in their testem.json config.
*
* @param {Object} commandOptions
* @override
*/
_generateCustomConfigs(commandOptions) {
const config = this._super._generateCustomConfigs.apply(this, arguments);
let additionalEvents = this._setupAndGetBrowserSocketEvents(config);
if (commandOptions.loadBalance || commandOptions.replayExecution) {
const loadBalancingEvents = this._getLoadBalancingBrowserSocketEvents({
isLoadBalance: this.commands.get('loadBalance'),
isReplayExecution: this.commands.get('replayExecution'),
isWriteExecutionFile: this.commands.get('writeExecutionFile')
}, this.testemEvents );
additionalEvents = Object.assign(additionalEvents, loadBalancingEvents);
}
config.custom_browser_socket_events = Object.assign(config.custom_browser_socket_events || {}, additionalEvents);
if (!commandOptions.loadBalance && !commandOptions.replayExecution && !commandOptions.parallel )
return config;
config.testPage = getMultipleTestPages(config, commandOptions);
if (commandOptions.replayExecution) {
this.testemEvents.setReplayExecutionMap(
commandOptions.replayExecution,
commandOptions.replayBrowser
);
}
return config;
},
/**
* Returns an event object to enable to send and receive module metadata
*
* @param {Object} config
*/
_setupAndGetBrowserSocketEvents(config) {
const commands = this.commands;
const testemEvents = this.testemEvents;
const ui = this.ui;
const browserExitHandler = function(failed = false) {
const launcherId = this.launcher.id;
if (!failed && commands.get('loadBalance')) {
try {
const browserId = getBrowserId(this.launcher);
log.info(`Browser ${browserId} exiting. [ # of modules in current module queue ${testemEvents.stateManager.getTestModuleQueue().length} ]`);
} catch (err) {
const moduleQueueMessage = testemEvents.stateManager.getTestModuleQueue() === null ?
'testModuleQueue is not set.' :
`[ # of modules in current module queue ${testemEvents.stateManager.getTestModuleQueue().length} ]`;
if (typeof err === 'object' && err !== null) {
err.message = `${err.message} \n ${moduleQueueMessage}`;
ui.writeLine(err.message);
} else {
throw new Error(moduleQueueMessage);
}
}
}
// config.testPage is undefined when parallization options are not used
// Set browserCount default value to 1 to allow
let browserCount = 1;
// When using multiple browsers config.testPage is an array of test page urls.
if (typeof config.testPage !== 'undefined') {
browserCount = Object.keys(config.testPage).length;
}
testemEvents.completedBrowsersHandler(
browserCount,
launcherId,
ui,
commands,
Date.now()
);
};
const browserTerminationHandler = function() {
// browserTerminationHandler is called for disconnect, processError or processExit events.
// disconnect and processExit events is fired during global error and successful test runs.
// On successful test runs, browserExitHandler should already be called. And is unnecessary
// to call it again, so we should return. This is covered by this.finish = true
// On global failure cases, it's possible that this.finish is also true. So we must check
// the timers set by onProcessExit
// https://github.com/testem/testem/blob/master/lib/runners/browser_test_runner.js#L266
// or onProcessError in testem.
// https://github.com/testem/testem/blob/master/lib/runners/browser_test_runner.js#L252
// If either timers is set, we should record the failed browser and call browserExitHandler
if (this.finished && (!this.onProcessExitTimer && !this.pendingTimer)) {
return;
}
if (commands.get('writeExecutionFile')) {
testemEvents.recordFailedBrowserId(this.launcher, ui);
}
browserExitHandler.call(this, true);
};
return this._getModuleMetadataAndBrowserExitSocketEvents(browserExitHandler, browserTerminationHandler);
},
/**
* Add browser socket events are needed for both with load-balance and without load-balance
*
* @param {Object} browserExitHandler
* @param {Object} browserTerminationHandler
*/
_getModuleMetadataAndBrowserExitSocketEvents(browserExitHandler, browserTerminationHandler) {
const events = {};
const testemEvents = this.testemEvents;
let init = false;
events['tests-start'] = function() {
if (!init) {
// process object is instantiated only when browsers are launched by testem server.
// 1. `ember test/exam` where browsers are instantiated by testem - process is available
// 2. `ember test/exam --server` where browsers can be instantiated by testem or manually
// - process is available only when browsers are instantiated by testem
// 3. `ember test/exam --serve --no-launch` where browsers are instantiated manually - process is undefined
// 4. `ember serve` where browsers are instantiated manually by developer - process is available.
if (typeof this.process !== 'undefined' && this.process !== null) {
this.process.on('processExit', browserTerminationHandler.bind(this));
this.process.on('processError', browserTerminationHandler.bind(this));
}
init = true;
}
if (typeof this.launcher !== 'undefined' && this.launcher !== null) {
testemEvents.recordStartedLauncherId(this.launcher.id);
}
};
events['after-tests-complete'] = browserExitHandler;
events['disconnect'] = function() {
// To prevent handling exiting browser browser disconnects from errors `disconnect` callback's needed to be registered.
browserTerminationHandler.bind(this)();
}
events['testem:test-done-metadata'] = (details) => {
// Ensure module detail is available
if (typeof details === 'object' && details !== null) {
//store module name, test name, # of failed assertion, and duration.
this.testemEvents.recordModuleMetadata(
{
moduleName: details.module,
testName: details.name,
passed: details.passed == details.total,
failed: (details.failed > 0),
skipped: details.skipped,
duration: details.runtime
}
)
}
}
return events;
},
/**
* Return an event object which enables load balancing.
* These event handlers will be registered on Testem's browserTestRunner socket instance
*
* @param {Object} commands
* @param {Object} testemEvents
*/
_getLoadBalancingBrowserSocketEvents({ isLoadBalance, isReplayExecution, isWriteExecutionFile } , testemEvents) {
const events = {};
const ui = this.ui;
events['testem:set-modules-queue'] = function(modules, browserId) {
testemEvents.setModuleQueue(
browserId,
modules,
isLoadBalance,
isReplayExecution
);
};
events['testem:next-module-request'] = function(browserId) {
testemEvents.nextModuleResponse(
browserId,
this.socket,
isWriteExecutionFile
);
};
events['test-result'] = function(result) {
if (result.failed && isWriteExecutionFile) {
testemEvents.recordFailedBrowserId(this.launcher, ui);
}
};
return events;
}
});