Skip to content
File

Blob: vitest.config.ts

typescript75 lines
1// @ts-nocheck
2import { cloudflareTest } from "@cloudflare/vitest-pool-workers";
3import { readFileSync, existsSync } from "node:fs";
4import path from "path";
5import { BASE_TEST_BINDINGS } from "./test/vitest.bindings";
6import { defineConfig } from "vitest/config";
7 
8const AUTH_TEST_FILE = "test/auth.worker.test.ts";
9const INLINE_DEPS = ["@noble/hashes", "pako"];
10const isAuthSuite =
11 process.env.npm_lifecycle_event === "test:auth" ||
12 process.argv.some((arg) => arg.includes("auth.worker.test.ts"));
13 
14export default defineConfig({
15 plugins: [
16 cloudflareTest({
17 main: "./src/worker/index.ts",
18 wrangler: {
19 configPath: "./wrangler.jsonc",
20 },
21 
22 // Windows workaround: disable isolatedStorage to avoid EBUSY teardown failures
23 // Avoid filesystem persistence to prevent SQLite CANTOPEN/EBUSY on Windows
24 singleWorker: true,
25 isolatedStorage: false,
26 
27 miniflare: {
28 durableObjectsPersist: false,
29 kvPersist: false,
30 r2Persist: false,
31 cachePersist: false,
32 // Keep the Worker test runtime aligned with the deployed Worker.
33 compatibilityDate: "2026-05-13",
34 compatibilityFlags: ["nodejs_als"],
35 bindings: {
36 ...BASE_TEST_BINDINGS,
37 PACK_INDEXER_FIXTURE: process.env.PACK_INDEXER_FIXTURE === "1" ? "1" : "",
38 },
39 serviceBindings: {
40 // Service binding that runs in Node.js (not workerd) for reading
41 // fixture files from disk. Tests call env.FIXTURE_READER.fetch()
42 // with the file path as the URL pathname.
43 async FIXTURE_READER(request: Request) {
44 const url = new URL(request.url);
45 const filePath = decodeURIComponent(url.pathname.slice(1));
46 const resolved = path.resolve(__dirname, filePath);
47 if (!existsSync(resolved)) {
48 return new Response("not found", { status: 404 });
49 }
50 const data = readFileSync(resolved);
51 return new Response(data, {
52 headers: { "Content-Length": String(data.byteLength) },
53 });
54 },
55 },
56 },
57 }),
58 ],
59 resolve: {
60 alias: {
61 "@": path.resolve(__dirname, "./src"),
62 },
63 },
64 // Inline deps at the vite-node server level to avoid SSR optimizer resolution issues
65 server: {
66 deps: {
67 inline: INLINE_DEPS,
68 },
69 },
70 test: {
71 include: isAuthSuite ? [AUTH_TEST_FILE] : ["test/**/*.worker.test.ts"],
72 exclude: isAuthSuite ? [] : [AUTH_TEST_FILE],
73 },
74});