@cloudflare/vitest-plugin 0.0.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # `@cloudflare/vitest-plugin`
2
+
3
+ The Workers Vitest integration allows you to write Vitest tests that run inside the Workers runtime.
4
+ Refer to the [documentation](https://developers.cloudflare.com/workers/testing/vitest-integration/) and [examples](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-plugin-examples/) for more information.
5
+
6
+ - ✅ Supports both **unit tests** and **integration tests**
7
+ - 📚 Provides direct access to Workers runtime APIs and bindings
8
+ - đŸ“Ļ Implements isolated per-test storage
9
+ - đŸ”Ĩ Runs tests fully-locally using [Miniflare](https://miniflare.dev/)
10
+ - âšĄī¸ Leverages Vitest's hot-module reloading for near instant reruns
11
+ - â†Šī¸ Provides a declarative interface for mocking outbound requests
12
+ - 🧩 Supports projects with multiple Workers
@@ -0,0 +1,70 @@
1
+ import { createRequire } from "node:module";
2
+
3
+ //#region rolldown:runtime
4
+ var __create$1 = Object.create;
5
+ var __defProp$1 = Object.defineProperty;
6
+ var __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames$1 = Object.getOwnPropertyNames;
8
+ var __getProtoOf$1 = Object.getPrototypeOf;
9
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
10
+ var __commonJS$1 = (cb, mod) => function() {
11
+ return mod || (0, cb[__getOwnPropNames$1(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
12
+ };
13
+ var __copyProps$1 = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames$1(from), i = 0, n = keys.length, key; i < n; i++) {
15
+ key = keys[i];
16
+ if (!__hasOwnProp$1.call(to, key) && key !== except) __defProp$1(to, key, {
17
+ get: ((k) => from[k]).bind(null, key),
18
+ enumerable: !(desc = __getOwnPropDesc$1(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM$1 = (mod, isNodeMode, target) => (target = mod != null ? __create$1(__getProtoOf$1(mod)) : {}, __copyProps$1(isNodeMode || !mod || !mod.__esModule ? __defProp$1(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+ var __require$1 = /* @__PURE__ */ createRequire(import.meta.url);
28
+
29
+ //#endregion
30
+ //#region ../workers-utils/dist/chunk-Q72B4Q5Z.mjs
31
+ var __create = Object.create;
32
+ var __defProp = Object.defineProperty;
33
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
34
+ var __getOwnPropNames = Object.getOwnPropertyNames;
35
+ var __getProtoOf = Object.getPrototypeOf;
36
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
37
+ var __name = (target, value) => __defProp(target, "name", {
38
+ value,
39
+ configurable: true
40
+ });
41
+ var __require = /* @__PURE__ */ ((x) => typeof __require$1 !== "undefined" ? __require$1 : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof __require$1 !== "undefined" ? __require$1 : a)[b] }) : x)(function(x) {
42
+ if (typeof __require$1 !== "undefined") return __require$1.apply(this, arguments);
43
+ throw Error("Dynamic require of \"" + x + "\" is not supported");
44
+ });
45
+ var __commonJS = (cb, mod) => function __require2() {
46
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
47
+ };
48
+ var __export = (target, all) => {
49
+ for (var name in all) __defProp(target, name, {
50
+ get: all[name],
51
+ enumerable: true
52
+ });
53
+ };
54
+ var __copyProps = (to, from, except, desc) => {
55
+ if (from && typeof from === "object" || typeof from === "function") {
56
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
57
+ get: () => from[key],
58
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
59
+ });
60
+ }
61
+ return to;
62
+ };
63
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
64
+ value: mod,
65
+ enumerable: true
66
+ }) : target, mod));
67
+
68
+ //#endregion
69
+ export { __toESM as a, __toESM$1 as c, __require as i, __export as n, __commonJS$1 as o, __name as r, __require$1 as s, __commonJS as t };
70
+ //# sourceMappingURL=chunk-Q72B4Q5Z-izl5Qmnx.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunk-Q72B4Q5Z-izl5Qmnx.mjs","names":[],"sources":["../../../workers-utils/dist/chunk-Q72B4Q5Z.mjs"],"sourcesContent":["var __create = Object.create;\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __getProtoOf = Object.getPrototypeOf;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __name = (target, value) => __defProp(target, \"name\", { value, configurable: true });\nvar __require = /* @__PURE__ */ ((x) => typeof require !== \"undefined\" ? require : typeof Proxy !== \"undefined\" ? new Proxy(x, {\n get: (a, b) => (typeof require !== \"undefined\" ? require : a)[b]\n}) : x)(function(x) {\n if (typeof require !== \"undefined\") return require.apply(this, arguments);\n throw Error('Dynamic require of \"' + x + '\" is not supported');\n});\nvar __commonJS = (cb, mod) => function __require2() {\n return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;\n};\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(\n // If the importer is in node compatibility mode or this is not an ESM\n // file that has been converted to a CommonJS file using a Babel-\n // compatible transform (i.e. \"__esModule\" has not been set), then set\n // \"default\" to the CommonJS \"module.exports\" for node compatibility.\n isNodeMode || !mod || !mod.__esModule ? __defProp(target, \"default\", { value: mod, enumerable: true }) : target,\n mod\n));\n\nexport { __commonJS, __export, __name, __require, __toESM };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAI,WAAW,OAAO;AACtB,IAAI,YAAY,OAAO;AACvB,IAAI,mBAAmB,OAAO;AAC9B,IAAI,oBAAoB,OAAO;AAC/B,IAAI,eAAe,OAAO;AAC1B,IAAI,eAAe,OAAO,UAAU;AACpC,IAAI,UAAU,QAAQ,UAAU,UAAU,QAAQ,QAAQ;CAAE;CAAO,cAAc;CAAM,CAAC;AACxF,IAAI,YAA4B,kBAAE,MAAM,uBAAmB,4BAAwB,OAAO,UAAU,cAAc,IAAI,MAAM,GAAG,EAC7H,MAAM,GAAG,OAAO,uBAAmB,4BAAwB,GAAG,IAC/D,CAAC,GAAG,GAAG,SAAS,GAAG;AAClB,KAAI,uBAAmB,YAAa,oBAAe,MAAM,MAAM,UAAU;AACzE,OAAM,MAAM,0BAAyB,IAAI,sBAAqB;EAC9D;AACF,IAAI,cAAc,IAAI,QAAQ,SAAS,aAAa;AAClD,QAAO,QAAQ,GAAG,GAAG,kBAAkB,GAAG,CAAC,MAAM,MAAM,EAAE,SAAS,EAAE,EAAE,EAAE,SAAS,IAAI,EAAE,IAAI;;AAE7F,IAAI,YAAY,QAAQ,QAAQ;AAC9B,MAAK,IAAI,QAAQ,IACf,WAAU,QAAQ,MAAM;EAAE,KAAK,IAAI;EAAO,YAAY;EAAM,CAAC;;AAEjE,IAAI,eAAe,IAAI,MAAM,QAAQ,SAAS;AAC5C,KAAI,QAAQ,OAAO,SAAS,YAAY,OAAO,SAAS,YACtD;OAAK,IAAI,OAAO,kBAAkB,KAAK,CACrC,KAAI,CAAC,aAAa,KAAK,IAAI,IAAI,IAAI,QAAQ,OACzC,WAAU,IAAI,KAAK;GAAE,WAAW,KAAK;GAAM,YAAY,EAAE,OAAO,iBAAiB,MAAM,IAAI,KAAK,KAAK;GAAY,CAAC;;AAExH,QAAO;;AAET,IAAI,WAAW,KAAK,YAAY,YAAY,SAAS,OAAO,OAAO,SAAS,aAAa,IAAI,CAAC,GAAG,EAAE,EAAE,YAKnG,cAAc,CAAC,OAAO,CAAC,IAAI,aAAa,UAAU,QAAQ,WAAW;CAAE,OAAO;CAAK,YAAY;CAAM,CAAC,GAAG,QACzG,IACD"}
@@ -0,0 +1,105 @@
1
+ import * as miniflare0 from "miniflare";
2
+ import { LegacyWorkerOptions, Miniflare, Request, Response, V4ModuleRule } from "miniflare";
3
+ import { z } from "zod";
4
+ import { PoolRunnerInitializer, TestProject, Vite, Vitest } from "vitest/node";
5
+ import { ProvidedContext } from "vitest";
6
+
7
+ //#region src/pool/config.d.ts
8
+ declare const WorkersPoolOptionsSchema: z.ZodObject<{
9
+ main: z.ZodOptional<z.ZodString>;
10
+ remoteBindings: z.ZodDefault<z.ZodBoolean>;
11
+ verbose: z.ZodOptional<z.ZodBoolean>;
12
+ additionalExports: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodLiteral<"WorkerEntrypoint">, z.ZodLiteral<"DurableObject">, z.ZodLiteral<"WorkflowEntrypoint">]>>>;
13
+ miniflare: z.ZodOptional<z.ZodObject<{
14
+ workers: z.ZodOptional<z.ZodArray<z.ZodObject<{}, z.core.$loose>>>;
15
+ }, z.core.$loose>>;
16
+ wrangler: z.ZodOptional<z.ZodObject<{
17
+ configPath: z.ZodOptional<z.ZodString>;
18
+ environment: z.ZodOptional<z.ZodString>;
19
+ }, z.core.$strip>>;
20
+ }, z.core.$strip>;
21
+ type CompatibleWorkerOptions = LegacyWorkerOptions & {
22
+ /** @deprecated Use `cacheAPI` instead. */
23
+ cache?: LegacyWorkerOptions["cacheAPI"];
24
+ };
25
+ type SourcelessWorkerOptions = Omit<CompatibleWorkerOptions, "script" | "scriptPath" | "modules" | "modulesRoot"> & {
26
+ modulesRules?: V4ModuleRule[];
27
+ };
28
+ type WorkersPoolOptions = z.input<typeof WorkersPoolOptionsSchema> & {
29
+ miniflare?: SourcelessWorkerOptions & {
30
+ workers?: CompatibleWorkerOptions[];
31
+ };
32
+ };
33
+ type WorkersPoolOptionsWithDefines = WorkersPoolOptions & {
34
+ defines?: Record<string, string>;
35
+ moduleRules?: V4ModuleRule[];
36
+ };
37
+ //#endregion
38
+ //#region src/pool/plugin.d.ts
39
+ type ProvidedContextKeys = keyof ProvidedContext & string;
40
+ declare const explicitInjectTypeArgumentRequired: unique symbol;
41
+ type ExplicitInjectTypeArgumentRequired = {
42
+ readonly [explicitInjectTypeArgumentRequired]: never;
43
+ };
44
+ type WorkerPoolOptionsContextInject = [ProvidedContextKeys] extends [never] ? <T = unknown>(key: string) => T : {
45
+ <K extends ProvidedContextKeys>(key: K): ProvidedContext[K];
46
+ <T = ExplicitInjectTypeArgumentRequired>(key: string & (T extends ExplicitInjectTypeArgumentRequired ? never : unknown)): T;
47
+ };
48
+ interface WorkerPoolOptionsContext {
49
+ /**
50
+ * Access values provided by `globalSetup()` (e.g. ports of servers started
51
+ * during setup) for use in Miniflare options (e.g. bindings, upstream,
52
+ * hyperdrives, ...).
53
+ *
54
+ * Known `ProvidedContext` keys preserve Vitest's inference and key checking
55
+ * when the consuming project's augmentation is visible. Use an explicit type
56
+ * argument for keys provided at runtime but not declared in `ProvidedContext`.
57
+ * If pnpm resolves a separate Vitest copy and the keys collapse to `never`,
58
+ * fall back to the wider inject signature instead.
59
+ */
60
+ inject: WorkerPoolOptionsContextInject;
61
+ }
62
+ declare function cloudflareTest(options: WorkersPoolOptions | ((ctx: WorkerPoolOptionsContext) => Promise<WorkersPoolOptions> | WorkersPoolOptions)): Vite.Plugin;
63
+ //#endregion
64
+ //#region src/pool/pool.d.ts
65
+ declare function cloudflarePool(poolOptions: WorkersPoolOptions | ((ctx: WorkerPoolOptionsContext) => Promise<WorkersPoolOptions> | WorkersPoolOptions)): PoolRunnerInitializer;
66
+ //#endregion
67
+ //#region src/shared/d1.d.ts
68
+ type D1Migration = {
69
+ name: string;
70
+ queries: string[];
71
+ };
72
+ //#endregion
73
+ //#region src/pool/d1.d.ts
74
+ /**
75
+ * Reads all migrations in `migrationsPath`, ordered by migration number.
76
+ * Each migration will have its contents split into an array of SQL queries.
77
+ */
78
+ declare function readD1Migrations(migrationsPath: string): Promise<D1Migration[]>;
79
+ //#endregion
80
+ //#region src/pool/pages.d.ts
81
+ declare function poolWorkerStarted(): void;
82
+ declare function poolWorkerStopped(): void;
83
+ declare function buildPagesASSETSBinding(assetsPath: string): Promise<(request: Request) => Promise<Response>>;
84
+ //#endregion
85
+ //#region src/pool/index.d.ts
86
+ declare function structuredSerializableStringify(value: unknown): string;
87
+ declare function structuredSerializableParse(value: string): unknown;
88
+ declare function getRunnerName(project: TestProject, testFile?: string): string;
89
+ interface DurableObjectDesignator {
90
+ className: string;
91
+ scriptName?: string;
92
+ unsafeUniqueKey?: string;
93
+ }
94
+ /**
95
+ * Returns a map of Durable Objects bindings' bound names to the designators of
96
+ * the objects they point to.
97
+ */
98
+ declare function getDurableObjectDesignators(options: WorkersPoolOptions): Map<string, DurableObjectDesignator>;
99
+ declare function getProjectMiniflare(ctx: Vitest, project: TestProject, poolOptions: WorkersPoolOptionsWithDefines, main: string | undefined): Promise<Miniflare>;
100
+ declare function maybeGetResolvedMainPath(project: TestProject, options: WorkersPoolOptionsWithDefines): string | undefined;
101
+ declare function connectToMiniflareSocket(mf: Miniflare, workerName: string): Promise<miniflare0.WebSocket>;
102
+ declare function assertCompatibleVitestVersion(ctx: Vitest): void;
103
+ //#endregion
104
+ export { type D1Migration, assertCompatibleVitestVersion, buildPagesASSETSBinding, cloudflarePool, cloudflareTest, connectToMiniflareSocket, getDurableObjectDesignators, getProjectMiniflare, getRunnerName, maybeGetResolvedMainPath, poolWorkerStarted, poolWorkerStopped, readD1Migrations, structuredSerializableParse, structuredSerializableStringify };
105
+ //# sourceMappingURL=index.d.mts.map