vitest-plugin: waitOnExecutionContext() doesn't wait for imported waitUntil

Open
#16,715 1 comment 0 reactions 0 assignees View on GitHub

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
Domain
devtools, testing

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

bug package:vitest
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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workers-sdk

All issues in cloudflare/workers-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.