Skip to content

Commit

Permalink
POC for basic web platform test integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Aug 22, 2024
1 parent 2c4fea3 commit e5803d7
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 0 deletions.
11 changes: 11 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,14 @@ new_local_repository(
visibility = ["//visibility:public"],)""",
path = "empty",
)

# ========================================================================================
# Web Platform Tests

http_archive(
name = "wpt",
build_file = "//:build/BUILD.wpt",
integrity = "sha256-6mp9wOEkWuACXvSDmf1pL2Dlj5ruukYwQzwkNgcpCdI=",
strip_prefix = "wpt-merge_pr_47718",
url = "https://github.com/web-platform-tests/wpt/archive/refs/tags/merge_pr_47718.tar.gz",
)
8 changes: 8 additions & 0 deletions build/BUILD.wpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
filegroup(
name = "url",
srcs = glob(
include = ["url/**/*"],
allow_empty = True,
),
visibility = ["//visibility:public"],
)
12 changes: 12 additions & 0 deletions src/workerd/api/wpt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//:build/wd_test.bzl", "wd_test")

wd_test(
name = "url-test",
src = "url-test.wd-test",
args = ["--experimental"],
data = [
"url-test.js",
"//src/wpt:wpt-test-harness",
"@wpt//:url",
],
)
23 changes: 23 additions & 0 deletions src/workerd/api/wpt/url-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { strictEqual } from 'assert';

// The harness has no exports of it's own but does modify the global
// scope to include the additional stuff the Web Platform Tests expect.
import * as harness from 'harness';

export const test = {
async test() {
// The tests will be run when the module is imported. Note that
// there are limitations to this in that the module is not allowed
// to perform any I/O... so any test that requires setTimeout,
// for instance, will fail... this also means we cannot run
// fetch tests from WPT... sad face.
const foo = await import('url-origin.any.js');

if (globalThis.errors.length > 0) {
for (const err of globalThis.errors) {
console.error(err);
}
throw new Error('Test failed');
}
},
};
30 changes: 30 additions & 0 deletions src/workerd/api/wpt/url-test.wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "url-test-test",
worker = (
modules = [
(name = "worker", esModule = embed "url-test.js"),
(name = "harness",
esModule = embed "../../../../../workerd/src/wpt/harness.js"),
(name = "url-origin.any.js",
esModule = embed "../../../../../wpt/url/url-origin.any.js"),
(name = "resources/urltestdata.json",
json = embed "../../../../../wpt/url/resources/urltestdata.json"),
(name = "resources/urltestdata-javascript-only.json",
json = embed "../../../../../wpt/url/resources/urltestdata-javascript-only.json"),
],
bindings = [
(name = "wpt", service = "wpt"),
],
compatibilityDate = "2024-07-01",
compatibilityFlags = ["nodejs_compat_v2"],
)
),
(
name = "wpt",
disk = ".",
)
],
);
8 changes: 8 additions & 0 deletions src/wpt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
filegroup(
name = "wpt-test-harness",
srcs = glob(
include = ["**/*"],
allow_empty = True,
),
visibility = ["//visibility:public"],
)
29 changes: 29 additions & 0 deletions src/wpt/harness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { strictEqual } from 'node:assert';

globalThis.fetch = async (url) => {
const { default: data } = await import(url);
return {
async json() {
return data;
},
};
};

globalThis.promise_test = (callback) => {
callback();
};

globalThis.assert_equals = (a, b, c) => {
strictEqual(a, b, c);
};

globalThis.test = (callback, message) => {
try {
callback();
} catch (err) {
const aerr = new AggregateError([err], message);
globalThis.errors.push(aerr);
}
};

globalThis.errors = [];

0 comments on commit e5803d7

Please sign in to comment.