The Workers Vitest plugin has the following known issues:
Native code coverage via V8 ↗︎ is not supported. You must use instrumented code coverage via Istanbul ↗︎ instead. Refer to the Vitest Coverage documentation ↗︎ for setup instructions.
Vitest's fake timers ↗︎ do not apply to KV, R2 and cache simulators. For example, you cannot expire a KV key by advancing fake time.
Fake timers change JavaScript time, including Date.now(), but do not change SQLite functions such as CURRENT_TIMESTAMP. To control timestamps in SQL, pass JavaScript time as a bound parameter:
const row = ctx.storage.sql.exec("SELECT ? AS now", Date.now()).one();const row = ctx.storage.sql.exec("SELECT ? AS now", Date.now()).one();Advancing fake time does not trigger Durable Object alarms. Use runDurableObjectAlarm() to trigger them explicitly.
Dynamic import() statements do not work inside export default { ... } handlers when writing integration tests with exports.default.fetch(), or inside Durable Object event handlers. You must import and call your handlers directly, or use static import statements in the global scope.
Durable Object WebSockets support per-file storage isolation with @cloudflare/vitest-plugin and with @cloudflare/vitest-pool-workers 0.13.0 and later.
The legacy Vitest 3 pool requires shared storage for these tests. To use per-file isolation, first migrate from Vitest 3 to Vitest 4. You can then migrate to the Vitest plugin. To share state across test files, refer to Isolation model.
By default, each test file has its own local storage environment. Tests within the same file share local KV, R2, D1, Durable Object, and Cache API state.
The integration ignores the removed isolatedStorage and singleWorker options at runtime. TypeScript reports them as unknown properties when you specify them directly in a type-checked cloudflareTest() options object literal.
If tests in the same file affect each other, refer to Clean up state between tests in the same file for cleanup and isolation patterns.
When working with per-file storage isolation, Cloudflare recommends the following actions to avoid common issues:
Always await all Promises that read or write to storage services.
// Example: Seed data
beforeAll(async () => {
await env.KV.put("message", "test message");
await env.R2.put("file", "hello-world");
});When calling RPC methods of a Service Worker or Durable Object that return non-primitive values (such as objects or classes extending RpcTarget), use the using keyword to explicitly signal when resources can be disposed of. See this example test ↗︎ and refer to explicit-resource-management for more details.
using result = await stub.getCounter();When making requests via fetch or R2.get(), consume the entire response body, even if you are not asserting its content. For example:
test("check if file exists", async () => {
await env.R2.put("file", "hello-world");
const response = await env.R2.get("file");
expect(response).not.toBe(null);
// Consume the response body even if you are not asserting it
await response.text();
});The ctx.exports property provides access to the exports of the main Worker. The Workers Vitest integration attempts to automatically infer these exports by statically analyzing the Worker source code using esbuild. However, complex build setups, such as those using virtual modules or wildcard re-exports that esbuild cannot follow, may result in missing properties on the ctx.exports object.
For example, consider a Worker that re-exports an entrypoint from a virtual module using a wildcard export:
// index.ts
export * from "@virtual-module";In this case, any exports from @virtual-module (such as MyEntrypoint) cannot be automatically inferred and will be missing from ctx.exports.
To work around this, add the additionalExports option to your Vitest configuration:
import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";
export default defineConfig({
plugins: [
cloudflareTest({
wrangler: { configPath: "./wrangler.jsonc" },
additionalExports: {
MyEntrypoint: "WorkerEntrypoint",
},
}),
],
});The additionalExports option is a map where keys are the export names and values are the type of export ("WorkerEntrypoint", "DurableObject", or "WorkflowEntrypoint").
If you encounter module resolution issues such as: Error: Cannot use require() to import an ES Module or Error: No such module, you can bundle these dependencies using the deps.optimizer ↗︎ option:
import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";
export default defineConfig({
plugins: [
cloudflareTest({
// ...
}),
],
test: {
deps: {
optimizer: {
ssr: {
enabled: true,
include: ["your-package-name"],
},
},
},
},
});You can find an example in the Recipes page.
Although Vitest is set up to resolve packages for the workerd ↗︎ runtime, it runs your global setup file in the Node.js environment. This can cause issues when importing packages like Postgres.js ↗︎, which exports a non-Node version for workerd.
To work around this, you can create a wrapper that uses Vite's SSR module loader to import the global setup file under the correct conditions. Then, adjust your Vitest configuration to point to this wrapper. For example:
// File: global-setup-wrapper.ts
import { createServer } from "vite";
// Import the actual global setup file with the correct setup
const mod = await viteImport("./global-setup.ts");
export default mod.default;
// Helper to import the file with default node setup
async function viteImport(file: string) {
const server = await createServer({
root: import.meta.dirname,
configFile: false,
server: { middlewareMode: true, hmr: false, watch: null, ws: false },
optimizeDeps: { noDiscovery: true },
clearScreen: false,
});
const mod = await server.ssrLoadModule(file);
await server.close();
return mod;
}// File: vitest.config.ts
import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";
export default defineConfig({
plugins: [
cloudflareTest({
// ...
}),
],
test: {
// Replace the globalSetup with the wrapper file
globalSetup: ["./global-setup-wrapper.ts"],
},
});