service worker: Improve WPT tests for async respondWith/waitUntil.

See discussion at [1] and [2].

This makes the following changes.

Adds a test for:

self.addEventListener('fetch', e => {
  Promise.resolve().then(() => {
    e.respondWith(new Response('hi'));

This should not throw because respondWith() is called while the event
dispatch flag is still set.

The microtask checkpoint is in "Cleanup After Running Scripts" here:

This is called from step 16.2 here:

Which in turn is called from the DOM spec's "Inner Invoke" to call event

Changes the expectation for:

addEventListener('message', event => {

From throws to not throws, for the same reasoning as above.

Changes the expectation for:

addEventListener('message', event => {
  waitPromise = Promise.resolve();
  waitPromise.then(() => {
    Promise.resolve().then(() => {event.waitUntil();});

From throws to not throws. This is subtle. Because all the promises
are just Promise.resolve(), the event dispatch flag is still set
by the time the second waitUntil() is called.

To test what 3. originally intended, a new test is
added which makes waitPromise a promise that does not immediately

Changes the expectation for:

addEventListener(‘fetch’, event => {
  response = Promise.resolve(new Response('RESP'));
  response.then(() => {
    Promise.resolve().then(() => {event.waitUntil();});

Again this is because the promises used resolve immediately,
so the event dispatch flag is still set.

Similarly, a new test is added to cover the original intent.

These WPT changes appear to match the behavior of Safari and Edge while
diverging from Chrome and (partially) Firefox.


Bug: 942414
Change-Id: I9a4a56d71d3919ed614ff78df2bdc6cc0251dadd
Commit-Queue: Matt Falkenhagen <>
Reviewed-by: Ben Kelly <>
Cr-Commit-Position: refs/heads/master@{#641514}
6 files changed