Thanks to Ms2ger web-platform-tests is now even more awesome (not in the American sense). To avoid writing HTML boilerplate, web-platform-tests supports .window.js
, .worker.js
, and .any.js
resources, for writing JavaScript that needs to run in a window, dedicated worker, or both at once. I very much recommend using these resource formats as they ease writing and reviewing tests and ensure APIs get tested across globals.
Ms2ger extended .any.js
to also cover shared and service workers. To test all four globals, create a single your-test.any.js
resource:
// META: global=window,worker promise_test(async () => { const json = await new Response(1).json() assert_equals(json, 1); }, "Response object: very basic JSON parsing test");
And then you can load it from your-test.any.html
, your-test.any.worker.html
, your-test.any.sharedworker.html
, and your-test.https.any.serviceworker.html
(requires enabling HTTPS) to see the results of running that code in those globals.
The default globals for your-test.any.js
are a window and a dedicated worker. You can unset the default using !default
. So if you just want to run some code in a service worker:
// META: global=!default,serviceworker
Please give this a try and donate some tests for your favorite API annoyances.