question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FetchEvent#waitUntil in Jest context

See original GitHub issue

Problem

The promises passed to FetchEvent.prototype.waitUntil cannot be retrieved via Response.prototype.waitUntil(). There doesn’t seem to be any other way to access them either, but if there is, the Writing and Running Tests docs don’t mention it.

Configuration

// jest.config.js
module.exports = {
  testEnvironment: 'miniflare',
}

Example Code

describe('waitUntil', () => {
  it('caches things', async () => {
    async function myHandler(fetchEvent) {
      const { url } = fetchEvent.request
      fetchEvent.waitUntil(caches.default.put(url, new Response('written to cache')))
      return new Response('returned from worker')
    }

    // build a FetchEvent:
    const url = 'https://example.com/cache-something'
    const request = new Request(url)
    const fetchEvent = new FetchEvent('fetch', { request })

    // get a response for it:
    const response = await myHandler(fetchEvent)

    // the response should have the Promises from `fetchEvent.waitUntil`:
    const awaited = await response.waitUntil()
    expect(awaited[0]).toBeTruthy() // this fails; awaited is []
    expect(await awaited[0].text()).toBe('written to cache')

    // and the cache-write should have completed:
    const cachedResponse = await caches.default.match(url)
    expect(cachedResponse).toBeTruthy()
    expect(await cachedResponse.text()).toBe('written to cache')
  })
})

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
mrbbotcommented, Mar 4, 2022

Hey! 👋 Thanks for raising this. Will try get a getWaitUntil(event) method added and exposed to tests soon. 🙂

0reactions
jamesarosencommented, Sep 7, 2022

@CraigglesO I poked at this a bit. I’m glad you did it because your #345 looks much better than what I had!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ExtendableEvent.waitUntil() - Web APIs | MDN
waitUntil () method tells the event dispatcher that work is ongoing. It can also be used to detect whether that work was successful....
Read more >
How to make Jest wait for all asynchronous code to finish ...
This method is used to break up long running operations and run a callback function immediately after the browser has completed other operations ......
Read more >
FetchEvent · Cloudflare Workers docs
The waitUntil command extends the lifetime of the "fetch" event. It accepts a Promise -based task which the Workers runtime will execute before ......
Read more >
Testing Service Workers. In this post we'll be looking at some…
Real Fetch Events. First let's look at how to test fetch events in a manner that includes the full browser behavior of using...
Read more >
API Reference | Vitest
When a test function returns a promise, the runner will wait until it is resolved to collect async expectations. If the promise is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found