-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathqunit.js
462 lines (405 loc) · 14.4 KB
/
qunit.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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* grunt-contrib-qunit
* https://gruntjs.com/
*
* Copyright (c) 2016 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/
'use strict';
// Nodejs libs.
var fs = require('fs');
var path = require('path');
var url = require('url');
// NPM libs.
var EventEmitter = require('eventemitter2');
var puppeteer = require('puppeteer');
var Promise = global.Promise;
// Shared functions
// Allow an error message to retain its color when split across multiple lines.
function formatMessage (message) {
var str = String(message);
if (typeof message === 'object' && /^\[object .*\]$/.test(str)) {
// try to use the JSON as a better string representation
try {
str = JSON.stringify(message, null, 2);
} catch ( _ ) {
}
}
return String(str).split('\n')
.map(function(s) {
return s.magenta;
})
.join('\n');
}
function createRunEnd () {
return {
status: 'passed',
testCounts: {
passed: 0,
failed: 0,
skipped: 0,
todo: 0,
total: 0
},
runtime: 0
};
}
function combineRunEnd(combined, runEnd) {
if (runEnd.status === 'failed') {
combined.status = runEnd.status;
}
combined.testCounts.passed += runEnd.testCounts.passed;
combined.testCounts.failed += runEnd.testCounts.failed;
combined.testCounts.skipped += runEnd.testCounts.skipped;
combined.testCounts.todo += runEnd.testCounts.todo;
combined.testCounts.total += runEnd.testCounts.total;
combined.runtime += runEnd.runtime;
}
function generateMessage(combined) {
return [
combined.testCounts.total,
' tests completed in ',
combined.runtime,
'ms, with ',
combined.testCounts.failed,
' failed, ' +
combined.testCounts.skipped,
' skipped, and ',
combined.testCounts.todo,
' todo.'
].join('');
}
// Copied from QUnit source code
function generateHash (module) {
var hex;
var i = 0;
var hash = 0;
var str = module + '\x1C' + undefined;
var len = str.length;
for (; i < len; i++) {
hash = ((hash << 5) - hash) + str.charCodeAt(i);
hash |= 0;
}
// Convert the possibly negative integer hash code into an 8 character
// hex string, which isn't strictly necessary but increases user understanding
// that the id is a SHA-like hash
hex = (0x100000000 + hash).toString(16);
if (hex.length < 8) {
hex = '0000000' + hex;
}
return hex.slice(-8);
}
function getPath(url) {
if (url.substr( 0, 7 ) === 'http://' || url.substr( 0, 8 ) === 'https://') {
return url;
}
return 'file://' + path.resolve(process.cwd(), url);
}
module.exports = function(grunt) {
var eventBus = new EventEmitter({wildcard: true, maxListeners: 0});
// Keep track of the last-started module and test. Additionally, keep track
// of status for individual test files and the entire test suite.
var options;
var combinedRunEnd;
var failureBuffer = [];
var browser;
var page;
// Get an asset file, local to the root of the project.
var asset = path.join.bind(null, __dirname, '..');
// If options.force then log an error, otherwise exit with a warning
function warnUnlessForced (message) {
if (options && options.force) {
grunt.log.error(message);
} else {
grunt.warn(message);
}
}
function formatFailedAssertion(error) {
var failure = '' +
'Message: ' + formatMessage(error.message) + '\n' +
'Actual: ' + formatMessage(error.actual) + '\n' +
'Expected: ' + formatMessage(error.expected);
if (error.stack) {
failure += '\n' + error.stack.replace(/^\s+(at) /g, ' $1 ');
}
return failure;
}
function waitForNextRunEnd(url) {
return new Promise(function(resolve, reject) {
eventBus.once('qunit.on.runEnd', function() { resolve(); });
eventBus.once('fail.*', function() { reject(url); });
});
}
// QUnit events.
eventBus.on('qunit.on.testStart', function(testStart) {
var name = testStart.fullName.join(' > ');
grunt.verbose.write(name + '...');
});
eventBus.on('qunit.on.testEnd', function(testEnd) {
var testPassed = (testEnd.status !== 'failed');
if (testPassed) {
// plainly "passed", or "skipped", or expected-failing "todo".
//
// Either complete the verbose testStart line, or continue dot progress.
grunt.verbose.ok().or.write('.');
return;
}
if (options && options.summaryOnly) {
return;
}
var failure;
if (testEnd.status === 'todo') {
failure = 'Expected at least one failing assertion in todo test';
} else {
failure = testEnd.errors.map(formatFailedAssertion).join('\n');
}
if (grunt.option('verbose')) {
grunt.log.error();
grunt.log.error(failure);
} else {
var name = testEnd.fullName.join(' > ');
failureBuffer.push(name + '\n' + failure);
grunt.log.write('F'.red);
}
});
eventBus.on('qunit.on.runEnd', function(runEnd) {
if (!grunt.option('verbose')) {
// End the non-verbose dot progress line
if (runEnd.status === 'failed') {
grunt.log.writeln();
} else {
grunt.log.ok();
}
}
if (failureBuffer.length) {
grunt.log.error(failureBuffer.join('\n'));
failureBuffer.length = 0;
}
combineRunEnd(combinedRunEnd, runEnd);
});
// Re-broadcast qunit events on grunt.event.
eventBus.on('qunit.**', function() {
var args = [this.event].concat(grunt.util.toArray(arguments));
grunt.event.emit.apply(grunt.event, args);
});
// Built-in error handlers.
eventBus.on('fail.load', function(url) {
grunt.verbose.write('...');
grunt.event.emit('qunit.fail.load', url);
grunt.log.error('Chrome unable to load \'' + url + '\' URI.');
combinedRunEnd.status = 'failed';
});
eventBus.on('fail.timeout', function() {
grunt.log.writeln();
grunt.event.emit('qunit.fail.timeout');
grunt.log.error('Chrome timed out, possibly due to:\n' +
'- QUnit is not loaded correctly.\n- A missing QUnit start() call.\n' +
'- Or, a misconfiguration of this task.');
combinedRunEnd.status = 'failed';
});
eventBus.on('qunit.on.error', function (err) {
// It is the responsibility of QUnit to ensure a run is marked as failure
// if there are (unexpected) messages received from window.onerror.
//
// Prior to QUnit 2.17, details of global failures were printed by
// creating a fake test with "testEnd" event. Now, it is our responsiblity
// to print via `QUnit.on('error')`.
//
// NOTE: Avoid relying solely on Puppeteer's "pageerror" event, as that only
// catches the subset of execution errors that make their way to window.onerror.
// It misses out on unhandled rejections from the browser, the "No tests were run"
// internal QUnit error, and anything else reported to QUnit.onUncaughtException
// by QUnit plugins.
// https://qunitjs.com/api/extension/QUnit.onUncaughtException/
grunt.log.writeln();
grunt.log.error(err);
grunt.event.emit('qunit.error.onError', err);
});
eventBus.on('error.onError', function (msg) {
// This is important in addition to `QUnit.on('error')` to catch uncaught
// errors that happen before the bridge is in effect (which in practice
// will happen at DOMContentLoaded, after qunit.js and test files have done
// their initial execution, but before QUnit.begin event and any actual tests).
grunt.log.writeln();
grunt.log.error(msg.stack || msg);
grunt.event.emit('qunit.error.onError', msg);
});
grunt.registerMultiTask('qunit', 'Run QUnit tests in Headless Chrome.', function() {
// Chrome sandbox is incompatible with Docker and most CI environments
var defaultChromiumArgs = (
process.env.CHROMIUM_FLAGS || (process.env.CI ? '--no-sandbox' : '')
).split(' ');
// Merge task-specific and/or target-specific options with these defaults.
options = this.options({
// Default Chrome timeout.
timeout: 5000,
// QUnit-Chrome bridge file to be injected.
inject: asset('chrome/bridge.js'),
// Explicit non-file URLs to test.
urls: [],
force: false,
// Connect Chrome console output to Grunt output
console: true,
// Do not use an HTTP base by default
httpBase: false,
summaryOnly: false
});
var puppeteerLaunchOptions = Object.assign(
{
headless: true,
args: defaultChromiumArgs
},
options.puppeteer
);
// This task is asynchronous.
var done = this.async();
var urls;
// Read the content of the specified bridge files
var bridgeFiles = Array.isArray(options.inject) ? options.inject : [options.inject];
var bridgContents = [
"__grunt_contrib_qunit_timeout__ = " + JSON.stringify( options.timeout ) + ";"
];
for (var i = 0; i < bridgeFiles.length; i++) {
try {
bridgContents.push(fs.readFileSync(bridgeFiles[i], 'utf8'));
} catch (err) {
grunt.fail.fatal('Could not load the specified Chrome/QUnit bridge file: ' + bridgeFiles[i]);
}
}
if (options.httpBase) {
// If URLs are explicitly referenced, use them still
urls = options.urls;
// Then create URLs for the src files
this.filesSrc.forEach(function(testFile) {
urls.push(options.httpBase + '/' + testFile);
});
} else {
// Combine any specified URLs with src files.
urls = options.urls.concat(this.filesSrc);
}
// The final tasks to run before terminating the task
function finishTask(success) {
// Close the puppeteer browser
if (browser) {
browser.close();
}
// Finish the task
done(success);
}
function appendToUrls (queryParam, value) {
// Append the query param to all urls
urls = urls.map(function(testUrl) {
var parsed = url.parse(testUrl, true);
parsed.query[queryParam] = value;
delete parsed.search;
return url.format(parsed);
});
}
if (options.noGlobals) {
// Append a noglobal query string param to all urls
appendToUrls('noglobals', 'true');
}
if (grunt.option('modules')) {
var modules = grunt.option('modules').split(',');
var hashes = modules.map(function(module) {
return generateHash(module.trim());
});
// Append moduleId to all urls
appendToUrls('moduleId', hashes);
}
if (grunt.option('seed')) {
// Append seed to all urls
appendToUrls('seed', grunt.option('seed'));
}
// Reset combined data.
combinedRunEnd = createRunEnd();
// Instantiate headless browser
puppeteer.launch(puppeteerLaunchOptions)
.then(function(b) {
browser = b;
return b.newPage();
})
.then(function(p) {
page = p;
// emit events published in bridge.js.
// This function exposure survives url navigations.
return page.exposeFunction('__grunt_contrib_qunit__', function() {
eventBus.emit.apply(eventBus, [].slice.call(arguments));
});
})
.then(async function() {
// Pass through the console logs if instructed
if (options.console) {
page.on('console', function(msg) {
// The `msg` is a puppeteer.ConsoleMessage, which represents the console call
// including multple arguments passed to it.
//
// msg.text() formats the arguments into a naive newline-joined string, and
// includes error objects as a useless "JSHandle@error".
//
// msg.args() returns a JSHandle object for each argument, but all its
// evaluation features happen asynchronously via the browser, and in this
// event handler we can't await those easily as the grunt output will have
// moved on to other tests. If we want to print these, we'd have to refactor
// this so that the for-loop over "urls" below is aware of async code here.
// For now, we just let the useless "JSHandle" through and rely on developers
// to stringify any useful information ahead of time, e.g. `console.warn(String(err))`.
//
// Ref https://pptr.dev/#?product=Puppeteer&version=v9.0.0&show=api-class-consolemessage
var colors = {
'error': 'red',
'warning': 'yellow'
};
var txt = msg.text();
var color = colors[msg.type()];
grunt.log.writeln(color ? txt[color] : txt);
// grunt.log.writeln(`${msg.location().url}:${msg.location().lineNumber}`.gray); // debug
});
}
// Surface uncaught exceptions
// Ref https://pptr.dev/#?product=Puppeteer&version=v9.0.0&show=api-event-pageerror
page.on('pageerror', function(err) {
eventBus.emit('error.onError', err);
});
// Whenever a page is loaded with a new document, before scripts execute, inject the bridge file.
// Tell the client that when DOMContentLoaded fires, it needs to tell this
// script to inject the bridge. This should ensure that the bridge gets
// injected before any other DOMContentLoaded or window.load event handler.
page.evaluateOnNewDocument('if (window.QUnit) {\n' + bridgContents.join(";") + '\n} else {\n' + 'document.addEventListener("DOMContentLoaded", function() {\n' + bridgContents.join(";") + '\n});\n}\n');
for (const url of urls) {
// Reset current module.
grunt.event.emit('qunit.spawn', url);
grunt.verbose.subhead('Testing ' + url + ' ').or.write('Testing ' + url + ' ');
await Promise.all([
// Setup "once" listener for qunit.done / fail events
waitForNextRunEnd(url),
// Navigate to the url to be tested
page.goto(getPath(url), { timeout: options.timeout })
]);
}
})
.then(function() {
// All tests have been run.
var message = generateMessage(combinedRunEnd);
var success = (combinedRunEnd.status === 'passed');
// Log results.
if (!success) {
warnUnlessForced(message);
} else {
grunt.verbose.writeln();
grunt.log.ok(message);
}
if (!success && options && options.force) {
success = true;
}
// All done!
finishTask(success);
})
.catch(function(err) {
// If anything goes wrong, terminate the grunt task
grunt.log.error("There was an error with headless chrome");
grunt.fail.fatal(err);
finishTask(false);
});
});
};