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

Aperta
#16,715 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #16720 di @owreed — chiusa senza merge

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
javascript, node.js, typescript
Ambito
devtools, testing

Direzione di ricerca

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"].

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
TypeScript
Stelle
4.7k
Fork
1.7k
Merge medio
1g 22h
PR unite (30g)
282

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudflare/workers-sdk

Tutte le issue di cloudflare/workers-sdk

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.