Hey ๐Ÿ‘‹ For now you ll need to define

Hey! ๐Ÿ‘‹ For now, you'll need to define your own ExecutionContext class:
class ExecutionContext {
promises = [];
waitUntil(promise) { this.promises.push(promise); }
passThroughOnException() {}
}
...
const env = getMiniflareBindings();
const ctx = new ExecutionContext();
const response = await worker.fetch(request, env, ctx);
await Promise.all(ctx.promises);
class ExecutionContext {
promises = [];
waitUntil(promise) { this.promises.push(promise); }
passThroughOnException() {}
}
...
const env = getMiniflareBindings();
const ctx = new ExecutionContext();
const response = await worker.fetch(request, env, ctx);
await Promise.all(ctx.promises);
A better solution is in the works though. ๐Ÿ™‚
9 Replies
Ben Cox
Ben Coxโ€ข14mo ago
Hi there! Any progress on this? I've gotten pretty stuck trying to instantiate a Miniflare instance inside my Jest test using Module syntax but the only reason I need to is to pass a context to my handleRequest function.
Ben Cox
Ben Coxโ€ข14mo ago
@mrbbot Also, probably worth mentioning here: https://miniflare.dev/testing/jest - took me quite a while to find my way to these discord messages
๐Ÿคน Jest Environment ยท Miniflare
Fun, full-featured, fully-local simulator for Cloudflare Workers
Ben Cox
Ben Coxโ€ข14mo ago
Down at the bottom of the page it shows use of new ExecutionContext() but never mentions it's a roll-your-own situation.
MrBBot
MrBBotโ€ข14mo ago
Hey! ExecutionContext should be provided as a global class as of Miniflare 2.8.0 (https://github.com/cloudflare/miniflare/releases/tag/v2.8.0).
Ben Cox
Ben Coxโ€ข14mo ago
@mrbbot Do I need to add it to globals or something? My setup is based on the miniflare-typescript-esbuild-jest repository here: https://github.com/cloudflare/miniflare-typescript-esbuild-jest. Here's what I'm seeing when I try to use the code from the Jest page (https://miniflare.dev/testing/jest) without building it by hand... seems to think it's just a typescript type.
๐Ÿคน Jest Environment ยท Miniflare
Fun, full-featured, fully-local simulator for Cloudflare Workers
Ben Cox
Ben Coxโ€ข14mo ago
No description
Ben Cox
Ben Coxโ€ข14mo ago
Using 2.13.0
MrBBot
MrBBotโ€ข14mo ago
Hmmm, that's a good point. You could try something like...
declare global {
class ExecutionContext {
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
}
}
declare global {
class ExecutionContext {
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
}
}
...though I suspect that will conflict with the @cloudflare/workers-types type. Let me know if that works, if not, you might just need to use a // @ts-expect-error comment. ๐Ÿ˜ฆ In any case, this is definitely something we should fix. ๐Ÿ‘
Unknown User
Unknown Userโ€ข4mo ago
Message Not Public
Sign In & Join Server To View