Skip to content
File

Blob: tests/worker/helpers/env.ts

typescript14 lines
1import { env } from "cloudflare:workers";
2 
3export async function withEnvBinding<T>(name: keyof Env, value: string, run: () => Promise<T>): Promise<T> {
4 const bindings = env as unknown as Record<string, string | undefined>;
5 const previous = bindings[name as string];
6 bindings[name as string] = value;
7 try {
8 return await run();
9 } finally {
10 if (previous === undefined) delete bindings[name as string];
11 else bindings[name as string] = previous;
12 }
13}