Skip to content

Commit

Permalink
WIP: use Node built-in test runner with concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Dec 28, 2023
1 parent a82deab commit 518de92
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
"scripts": {
"prepare": "npm run convert-idl && npm run generate-js-globals",
"pretest": "npm run prepare && npm run init-wpt",
"test-wpt": "mocha test/web-platform-tests/run-wpts.js",
"test-tuwpt": "mocha test/web-platform-tests/run-tuwpts.js",
"test-wpt": "node --test test/web-platform-tests/run-wpts.js",
"test-tuwpt": "node --test test/web-platform-tests/run-tuwpts.js",
"test-mocha": "mocha",
"test-api": "mocha test/api",
"test": "mocha test/index.js",
Expand Down
58 changes: 29 additions & 29 deletions test/web-platform-tests/run-single-wpt.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict";
/* eslint-disable no-console */
const test = require("node:test");
const path = require("path");
const { URL } = require("url");
const { specify } = require("mocha-sugar-free");
const { JSDOM, VirtualConsole } = require("../../lib/api.js");
const ResourceLoader = require("../../lib/jsdom/browser/resources/resource-loader");
const { resolveReason } = require("./utils.js");
Expand All @@ -17,16 +17,16 @@ const unexpectedPassingTestMessage = `

module.exports = urlPrefixFactory => {
return (testPath, title = testPath, expectFail = false) => {
specify({
test(
title,
expectPromise: true,
// WPT also takes care of timeouts (maximum 60 seconds), this is an extra failsafe:
timeout: 70_000,
slow: 10_000,
fn() {
{
// WPT also takes care of timeouts (maximum 60 seconds), this is an extra failsafe:
timeout: 70_000
},
() => {
return createJSDOM(urlPrefixFactory(), testPath, expectFail);
}
});
);
};
};

Expand Down Expand Up @@ -54,19 +54,19 @@ class CustomResourceLoader extends ResourceLoader {
}
}

function formatFailedTest(test) {
switch (test.status) {
case test.PASS:
return `Unexpected passing test: ${JSON.stringify(test.name)}${unexpectedPassingTestMessage}`;
case test.FAIL:
case test.PRECONDITION_FAILED:
return `Failed in ${JSON.stringify(test.name)}:\n${test.message}\n\n${test.stack}`;
case test.TIMEOUT:
return `Timeout in ${JSON.stringify(test.name)}:\n${test.message}\n\n${test.stack}`;
case test.NOTRUN:
return `Uncompleted test ${JSON.stringify(test.name)}:\n${test.message}\n\n${test.stack}`;
function formatFailedTest(t) {
switch (t.status) {
case t.PASS:
return `Unexpected passing test: ${JSON.stringify(t.name)}${unexpectedPassingTestMessage}`;
case t.FAIL:
case t.PRECONDITION_FAILED:
return `Failed in ${JSON.stringify(t.name)}:\n${t.message}\n\n${t.stack}`;
case t.TIMEOUT:
return `Timeout in ${JSON.stringify(t.name)}:\n${t.message}\n\n${t.stack}`;
case t.NOTRUN:
return `Uncompleted test ${JSON.stringify(t.name)}:\n${t.message}\n\n${t.stack}`;
default:
throw new RangeError(`Unexpected test status: ${test.status} (test: ${JSON.stringify(test.name)})`);
throw new RangeError(`Unexpected test status: ${t.status} (test: ${JSON.stringify(t.name)})`);
}
}

Expand Down Expand Up @@ -167,17 +167,17 @@ function createJSDOM(urlPrefix, testPath, expectFail) {
assertThrowsJSImpl(constructor, func, description, "assert_throws_js");
};
// eslint-disable-next-line camelcase
window.promise_rejects_js = (test, expected, promise, description) => {
return promise.then(test.unreached_func("Should have rejected: " + description)).catch(e => {
window.promise_rejects_js = (t, expected, promise, description) => {
return promise.then(t.unreached_func("Should have rejected: " + description)).catch(e => {
assertThrowsJSImpl(expected, () => {
throw e;
}, description, "promise_reject_js");
});
};

window.add_result_callback(test => {
if (test.status === test.FAIL || test.status === test.TIMEOUT || test.status === test.NOTRUN) {
errors.push(formatFailedTest(test));
window.add_result_callback(t => {
if (t.status === t.FAIL || t.status === t.TIMEOUT || t.status === t.NOTRUN) {
errors.push(formatFailedTest(t));
}
});

Expand Down Expand Up @@ -212,13 +212,13 @@ function createJSDOM(urlPrefix, testPath, expectFail) {
resolve();
} else {
const unexpectedErrors = [];
for (const test of tests) {
const data = expectFail[test.name];
for (const t of tests) {
const data = expectFail[t.name];
const reason = data && data[0];

const innerExpectFail = resolveReason(reason) === "expect-fail";
if (innerExpectFail ? test.status === test.PASS : test.status !== test.PASS) {
unexpectedErrors.push(formatFailedTest(test));
if (innerExpectFail ? t.status === t.PASS : t.status !== test.PASS) {
unexpectedErrors.push(formatFailedTest(t));
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/web-platform-tests/run-tuwpts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
const { describe, before, after } = require("node:test");
const path = require("path");
const { describe, before, after } = require("mocha-sugar-free");
const { spawnSync } = require("child_process");
const { readManifest, getPossibleTestFilePaths } = require("./wpt-manifest-utils.js");
const wptServer = require("./wpt-server.js");
Expand All @@ -21,17 +21,17 @@ const possibleTestFilePaths = getPossibleTestFilePaths(manifest);

let wptServerURL, serverProcess;
const runSingleWPT = require("./run-single-wpt.js")(() => wptServerURL);
before({ timeout: 30_000 }, async () => {
before(async () => {
const { urls, subprocess } = await wptServer.start({ toUpstream: true });
wptServerURL = urls[0];
serverProcess = subprocess;
});
}, { timeout: 30_000 });

after(() => {
killSubprocess(serverProcess);
});

describe("Local tests in web-platform-test format (to-upstream)", () => {
describe("Local tests in web-platform-test format (to-upstream)", { concurrency: true }, () => {
for (const test of possibleTestFilePaths) {
runSingleWPT(test);
}
Expand Down
10 changes: 5 additions & 5 deletions test/web-platform-tests/run-wpts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
const { describe, before, after, test } = require("node:test");
const path = require("path");
const fs = require("fs");
const jsYAML = require("js-yaml");
const { Minimatch } = require("minimatch");
const { describe, specify, before, after } = require("mocha-sugar-free");
const { readManifest, getPossibleTestFilePaths } = require("./wpt-manifest-utils.js");
const wptServer = require("./wpt-server.js");
const { resolveReason, killSubprocess } = require("./utils.js");
Expand Down Expand Up @@ -39,17 +39,17 @@ checkToRun();

let wptServerURL, serverProcess;
const runSingleWPT = require("./run-single-wpt.js")(() => wptServerURL);
before({ timeout: 30_000 }, async () => {
before(async () => {
const { urls, subprocess } = await wptServer.start({ toUpstream: false });
wptServerURL = urls[0];
serverProcess = subprocess;
});
}, { timeout: 30_000 });

after(() => {
killSubprocess(serverProcess);
});

describe("web-platform-tests", () => {
describe("web-platform-tests", { concurrency: 4 }, () => {
for (const toRunDoc of toRunDocs) {
describe(toRunDoc.DIR, () => {
for (const testFilePath of possibleTestFilePaths) {
Expand Down Expand Up @@ -77,7 +77,7 @@ describe("web-platform-tests", () => {

switch (resolveReason(reason)) {
case "skip": {
specify.skip(`[${reason}] ${testFile}`);
test.skip(`[${reason}] ${testFile}`);
break;
}

Expand Down

0 comments on commit 518de92

Please sign in to comment.