FetchEvent#waitUntil in Jest context
See original GitHub issueProblem
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:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey! 👋 Thanks for raising this. Will try get a
getWaitUntil(event)
method added and exposed to tests soon. 🙂@CraigglesO I poked at this a bit. I’m glad you did it because your #345 looks much better than what I had!