vitest-plugin: waitOnExecutionContext() doesn't wait for imported waitUntil
Maintainers usually reply within 1 day
Nobody has claimed this yet.
- #16720 by @owreed — closed without merging
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- javascript, node.js, typescript
Research direction
Start with src/index.js and src/index.test.js in the reproduction, then read patchAndRunWithHandlerContext in dist/worker/lib/cloudflare/test-internal.mjs and the implementation of registerHandlerAndGlobalWaitUntil. Trace how imported waitUntil is connected to the current handler context, then run the reproduction with npm install && npx vitest run. Done means waitOnExecutionContext(ctx) waits for the imported waitUntil promise and the test observes log as ["done"].
Written by the indexing model from the issue text.
Description
What versions & operating system are you using?
@cloudflare/vitest-plugin 1.5.1, vitest 5.0.5, wrangler 4.157.0, Node 26.13.0, macOS (arm64).
Please provide a link to a minimal reproduction
Inline below: five files, npm install && npx vitest run.
Describe the Bug
Since August 2025, waitUntil can be imported from cloudflare:workers, so code deep in an app can use it without being handed ctx. In tests, waitOnExecutionContext(ctx) waits only for promises passed to ctx.waitUntil. Work passed to the imported waitUntil is never awaited, so a test can't check its effect, a rejection doesn't fail the test, and late work spills into the next test.
The plugin tracks ctx.waitUntil on contexts from createExecutionContext() and on handler contexts it patches (patchAndRunWithHandlerContext in dist/worker/lib/cloudflare/test-internal.mjs). The imported function calls the native IoContext::current().addWaitUntil(), which the plugin never sees.
Expected: waitOnExecutionContext(ctx) also waits for work passed to the imported waitUntil while handling that request, the way registerHandlerAndGlobalWaitUntil already routes the plugin's own patched setTimeout promises to the current handler context.
src/index.js:
import { waitUntil } from "cloudflare:workers";
export const log = [];
export default {
async fetch() {
waitUntil(
new Promise((resolve) => setTimeout(resolve, 100)).then(() => {
log.push("done");
}),
);
return new Response("ok");
},
};
src/index.test.js:
import { createExecutionContext, env, waitOnExecutionContext } from "cloudflare:test";
import { expect, it } from "vitest";
import worker, { log } from "./index.js";
it("waits for work passed to the imported waitUntil", async () => {
const ctx = createExecutionContext();
await worker.fetch(new Request("https://example.com"), env, ctx);
await waitOnExecutionContext(ctx);
expect(log).toEqual(["done"]);
});
vitest.config.js:
import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";
export default defineConfig({
plugins: [cloudflareTest({ wrangler: { configPath: "./wrangler.jsonc" } })],
});
wrangler.jsonc:
{
"name": "wait-until-repro",
"main": "src/index.js",
"compatibility_date": "2026-11-01"
}
package.json:
{
"name": "wait-until-repro",
"private": true,
"type": "module",
"devDependencies": {
"@cloudflare/vitest-plugin": "1.5.1",
"vitest": "5.0.5",
"wrangler": "4.157.0"
}
}
The test fails with log still []. Changing the worker to async fetch(request, env, ctx) and ctx.waitUntil(...) makes it pass.
Please provide any relevant error logs
AssertionError: expected [] to deeply equal [ 'done' ]
- Expected
+ Received
- [
- "done",
- ]
+ []
❯ src/index.test.js:13:15
- Dominant language
- TypeScript
- Stars
- 4.7k
- Forks
- 1.7k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 282
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cloudflare/workers-sdk
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
cloudflare/workers-sdk#17215 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
cloudflare/workers-sdk#17189 · 3 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cloudflare/workers-sdk#17187 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
cloudflare/workers-sdk#17175 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
cloudflare/workers-sdk#17174 ·
Maintainers usually reply within 1 day
All issues in cloudflare/workers-sdk
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
earendil-works/pi#12615 ·
Maintainers usually reply within 1 day
-
My Caps shows "Shared" on private Caps that were never sharedPossibly taken @AksharP5 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
CapSoftware/Cap#4569 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
OHDSI/Data2Evidence#3749 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Flagsmith/flagsmith-nodejs-client#332 ·
Maintainers usually reply within 2 days