• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Runs a collection of tests that determine if an API implements structured clone
3 * correctly.
4 *
5 * The `runner` parameter has the following properties:
6 * - `setup()`: An optional function run once before testing starts
7 * - `teardown()`: An option function run once after all tests are done
8 * - `preTest()`: An optional, async function run before a test
9 * - `postTest()`: An optional, async function run after a test is done
10 * - `structuredClone(obj, transferList)`: Required function that somehow
11 *                                         structurally clones an object.
12 * - `hasDocument`: When true, disables tests that require a document. True by default.
13 */
14
15function runStructuredCloneBatteryOfTests(runner) {
16  const defaultRunner = {
17    setup() {},
18    preTest() {},
19    postTest() {},
20    teardown() {},
21    hasDocument: true
22  };
23  runner = Object.assign({}, defaultRunner, runner);
24
25  let setupPromise = runner.setup();
26  const allTests = structuredCloneBatteryOfTests.map(test => {
27
28    if (!runner.hasDocument && test.requiresDocument) {
29      return;
30    }
31
32    return new Promise(resolve => {
33      promise_test(async _ => {
34        test = await test;
35        await setupPromise;
36        await runner.preTest(test);
37        await test.f(runner)
38        await runner.postTest(test);
39        resolve();
40      }, test.description);
41    }).catch(_ => {});
42  });
43  Promise.all(allTests).then(_ => runner.teardown());
44}
45