@cloudflare/vitest-plugin 0.0.0 → 1.1.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,130 @@
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
+ experimental: z.ZodOptional<z.ZodObject<{
21
+ newConfig: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
22
+ configPath: z.ZodOptional<z.ZodString>;
23
+ }, z.core.$strip>]>>;
24
+ }, z.core.$strip>>;
25
+ }, z.core.$strip>;
26
+ type CompatibleWorkerOptions = LegacyWorkerOptions & {
27
+ /** @deprecated Use `cacheAPI` instead. */
28
+ cache?: LegacyWorkerOptions["cacheAPI"];
29
+ };
30
+ type SourcelessWorkerOptions = Omit<CompatibleWorkerOptions, "script" | "scriptPath" | "modules" | "modulesRoot"> & {
31
+ modulesRules?: V4ModuleRule[];
32
+ };
33
+ type WorkersPoolOptions = z.input<typeof WorkersPoolOptionsSchema> & {
34
+ miniflare?: SourcelessWorkerOptions & {
35
+ workers?: CompatibleWorkerOptions[];
36
+ };
37
+ };
38
+ type WorkersPoolOptionsWithDefines = WorkersPoolOptions & {
39
+ defines?: Record<string, string>;
40
+ moduleRules?: V4ModuleRule[];
41
+ /**
42
+ * Details of the configuration file these options were resolved from. Set
43
+ * while parsing; not a user-facing option. Undefined when the project
44
+ * configures the Worker entirely through `miniflare` options.
45
+ */
46
+ resolvedConfig?: {
47
+ /** Absolute path of the configuration file. */
48
+ path: string;
49
+ /**
50
+ * Whether that file is a `cloudflare.config.ts` rather than a Wrangler
51
+ * configuration file. Determines how config fields are named in errors.
52
+ */
53
+ newConfig: boolean;
54
+ /**
55
+ * The Worker name declared in the configuration file, before any
56
+ * environment name is appended. Self-referential service, tail and
57
+ * Workflow bindings are written against this name.
58
+ */
59
+ workerName: string | undefined;
60
+ };
61
+ };
62
+ //#endregion
63
+ //#region src/pool/plugin.d.ts
64
+ type ProvidedContextKeys = keyof ProvidedContext & string;
65
+ declare const explicitInjectTypeArgumentRequired: unique symbol;
66
+ type ExplicitInjectTypeArgumentRequired = {
67
+ readonly [explicitInjectTypeArgumentRequired]: never;
68
+ };
69
+ type WorkerPoolOptionsContextInject = [ProvidedContextKeys] extends [never] ? <T = unknown>(key: string) => T : {
70
+ <K extends ProvidedContextKeys>(key: K): ProvidedContext[K];
71
+ <T = ExplicitInjectTypeArgumentRequired>(key: string & (T extends ExplicitInjectTypeArgumentRequired ? never : unknown)): T;
72
+ };
73
+ interface WorkerPoolOptionsContext {
74
+ /**
75
+ * Access values provided by `globalSetup()` (e.g. ports of servers started
76
+ * during setup) for use in Miniflare options (e.g. bindings, upstream,
77
+ * hyperdrives, ...).
78
+ *
79
+ * Known `ProvidedContext` keys preserve Vitest's inference and key checking
80
+ * when the consuming project's augmentation is visible. Use an explicit type
81
+ * argument for keys provided at runtime but not declared in `ProvidedContext`.
82
+ * If pnpm resolves a separate Vitest copy and the keys collapse to `never`,
83
+ * fall back to the wider inject signature instead.
84
+ */
85
+ inject: WorkerPoolOptionsContextInject;
86
+ }
87
+ declare function cloudflareTest(options: WorkersPoolOptions | ((ctx: WorkerPoolOptionsContext) => Promise<WorkersPoolOptions> | WorkersPoolOptions)): Vite.Plugin;
88
+ //#endregion
89
+ //#region src/pool/pool.d.ts
90
+ declare function cloudflarePool(poolOptions: WorkersPoolOptions | ((ctx: WorkerPoolOptionsContext) => Promise<WorkersPoolOptions> | WorkersPoolOptions)): PoolRunnerInitializer;
91
+ //#endregion
92
+ //#region src/shared/d1.d.ts
93
+ type D1Migration = {
94
+ name: string;
95
+ queries: string[];
96
+ };
97
+ //#endregion
98
+ //#region src/pool/d1.d.ts
99
+ /**
100
+ * Reads all migrations in `migrationsPath`, ordered by migration number.
101
+ * Each migration will have its contents split into an array of SQL queries.
102
+ */
103
+ declare function readD1Migrations(migrationsPath: string): Promise<D1Migration[]>;
104
+ //#endregion
105
+ //#region src/pool/pages.d.ts
106
+ declare function poolWorkerStarted(): void;
107
+ declare function poolWorkerStopped(): boolean;
108
+ declare function buildPagesASSETSBinding(assetsPath: string): Promise<(request: Request) => Promise<Response>>;
109
+ //#endregion
110
+ //#region src/pool/index.d.ts
111
+ declare function structuredSerializableStringify(value: unknown): string;
112
+ declare function structuredSerializableParse(value: string): unknown;
113
+ declare function getRunnerName(project: TestProject, testFile?: string): string;
114
+ interface DurableObjectDesignator {
115
+ className: string;
116
+ scriptName?: string;
117
+ unsafeUniqueKey?: string;
118
+ }
119
+ /**
120
+ * Returns a map of Durable Objects bindings' bound names to the designators of
121
+ * the objects they point to.
122
+ */
123
+ declare function getDurableObjectDesignators(options: WorkersPoolOptions): Map<string, DurableObjectDesignator>;
124
+ declare function getProjectMiniflare(ctx: Vitest, project: TestProject, poolOptions: WorkersPoolOptionsWithDefines, main: string | undefined): Promise<Miniflare>;
125
+ declare function maybeGetResolvedMainPath(project: TestProject, options: WorkersPoolOptionsWithDefines): string | undefined;
126
+ declare function connectToMiniflareSocket(mf: Miniflare, workerName: string): Promise<miniflare0.WebSocket>;
127
+ declare function assertCompatibleVitestVersion(ctx: Vitest): void;
128
+ //#endregion
129
+ export { type D1Migration, assertCompatibleVitestVersion, buildPagesASSETSBinding, cloudflarePool, cloudflareTest, connectToMiniflareSocket, getDurableObjectDesignators, getProjectMiniflare, getRunnerName, maybeGetResolvedMainPath, poolWorkerStarted, poolWorkerStopped, readD1Migrations, structuredSerializableParse, structuredSerializableStringify };
130
+ //# sourceMappingURL=index.d.mts.map