diff --git a/docs/_writing-tests/testharness-api.md b/docs/_writing-tests/testharness-api.md index d750086c6a3a76..baa8772b814656 100644 --- a/docs/_writing-tests/testharness-api.md +++ b/docs/_writing-tests/testharness-api.md @@ -668,6 +668,17 @@ or a [`ServiceWorker`](https://slightlyoff.github.io/ServiceWorker/spec/service_ Once called, the containing document fetches all the tests from the worker and behaves as if those tests were running in the containing document itself. +`fetch_tests_from_worker` returns a promise that resolves once all the remote +tests have completed. This is useful if you're importing tests from multiple +workers and want to ensure they run in series: + +```js +(async function() { + await fetch_tests_from_worker(new Worker("worker-1.js")); + await fetch_tests_from_worker(new Worker("worker-2.js")); +})(); +``` + ## List of Assertions ## ### `assert_true(actual, description)` diff --git a/resources/testharness.js b/resources/testharness.js index 29dc6784459f6c..a48a0cfd3ec001 100644 --- a/resources/testharness.js +++ b/resources/testharness.js @@ -1780,6 +1780,12 @@ policies and contribution forms [3]. } }; + if (self.Promise) { + this.done = new Promise(function(resolve) { + this_obj.doneResolve = resolve; + }); + } + this.message_target.addEventListener("message", this.message_handler); } @@ -1829,6 +1835,10 @@ policies and contribution forms [3]. this.running = false; this.remote = null; this.message_target = null; + if (this.doneResolve) { + this.doneResolve(); + } + if (tests.all_done()) { tests.complete(); } @@ -2187,11 +2197,13 @@ policies and contribution forms [3]. return; } - this.pending_remotes.push(this.create_remote_worker(worker)); + var remoteContext = this.create_remote_worker(worker); + this.pending_remotes.push(remoteContext); + return remoteContext.done; }; function fetch_tests_from_worker(port) { - tests.fetch_tests_from_worker(port); + return tests.fetch_tests_from_worker(port); } expose(fetch_tests_from_worker, 'fetch_tests_from_worker');