Skip to content

Commit

Permalink
Remove dependency on p-each-series in favour of await in for-loop
Browse files Browse the repository at this point in the history
For Node 12+, this is now easy to do in plain JavaScript.
Ref https://github.com/sindresorhus/p-each-series/blob/v3.0.0/index.js.

Closes #181.
  • Loading branch information
Krinkle committed Jun 11, 2024
1 parent 6455669 commit 1b93f81
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"curly": true,
"eqeqeq": true,
"eqnull": true,
"esversion": 6,
"esversion": 8,
"immed": true,
"latedef": true,
"newcap": true,
Expand Down
12 changes: 0 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
},
"dependencies": {
"eventemitter2": "^6.4.9",
"p-each-series": "^2.2.0",
"puppeteer": "^22.0.0"
},
"devDependencies": {
Expand Down
31 changes: 17 additions & 14 deletions tasks/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
var fs = require('fs');
var path = require('path');
var url = require('url');
var EventEmitter = require('eventemitter2');
// NPM libs.
var pEachSeries = require('p-each-series');
var EventEmitter = require('eventemitter2');
var puppeteer = require('puppeteer');

var Promise = global.Promise;
Expand Down Expand Up @@ -147,6 +146,13 @@ module.exports = function(grunt) {
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(' > ');
Expand Down Expand Up @@ -352,7 +358,7 @@ module.exports = function(grunt) {
eventBus.emit.apply(eventBus, [].slice.call(arguments));
});
})
.then(function() {
.then(async function() {
// Pass through the console logs if instructed
if (options.console) {
page.on('console', function(msg) {
Expand All @@ -366,9 +372,9 @@ module.exports = function(grunt) {
// 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 pEachSeries() 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))`.
// 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 = {
Expand All @@ -394,22 +400,19 @@ module.exports = function(grunt) {
// 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');

return pEachSeries(urls, function(url) {
for (const url of urls) {
// Reset current module.
grunt.event.emit('qunit.spawn', url);
grunt.verbose.subhead('Testing ' + url + ' ').or.write('Testing ' + url + ' ');

return Promise.all([
// Setup listeners for qunit.done / fail events
new Promise(function(resolve, reject) {
eventBus.once('qunit.on.runEnd', function() { resolve(); });
eventBus.once('fail.*', function() { reject(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.
Expand Down

0 comments on commit 1b93f81

Please sign in to comment.