structured-clone-battery-of-tests-harness.js (1512B)
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 * Must return a promise. 13 * - `hasDocument`: When true, disables tests that require a document. True by default. 14 */ 15 16 function runStructuredCloneBatteryOfTests(runner) { 17 const defaultRunner = { 18 setup() {}, 19 preTest() {}, 20 postTest() {}, 21 teardown() {}, 22 hasDocument: true 23 }; 24 runner = Object.assign({}, defaultRunner, runner); 25 26 let setupPromise = runner.setup(); 27 const allTests = structuredCloneBatteryOfTests.map(test => { 28 29 if (!runner.hasDocument && test.requiresDocument) { 30 return; 31 } 32 33 return new Promise(resolve => { 34 promise_test(async t => { 35 test = await test; 36 await setupPromise; 37 await runner.preTest(test); 38 await test.f(runner, t) 39 await runner.postTest(test); 40 resolve(); 41 }, test.description); 42 }).catch(_ => {}); 43 }); 44 Promise.all(allTests).then(_ => runner.teardown()); 45 }