UNPKG

@cloudflare/vitest-pool-workers

Version:

Workers Vitest integration for writing Vitest unit and integration tests that run inside the Workers runtime

133 lines (132 loc) 6.83 kB
import * as miniflare0 from "miniflare"; import { Miniflare, ModuleRule, Request, Response, WorkerOptions } from "miniflare"; import "wrangler"; import { z } from "zod"; import { PoolRunnerInitializer, TestProject, Vite, Vitest } from "vitest/node"; import { inject } from "vitest"; //#region src/pool/config.d.ts declare const WorkersPoolOptionsSchema: z.ZodObject<{ /** * Entrypoint to Worker run in the same isolate/context as tests. This is * required to use `import { exports } from "cloudflare:workers"`, or Durable * Objects without an explicit `scriptName`. Note this goes through Vite * transforms and can be a TypeScript file. Note also * `import module from "<path-to-main>"` inside tests gives exactly the same * `module` instance as is used internally for the `SELF` and Durable Object * bindings. */ main: z.ZodOptional<z.ZodString>; /** * Enables remote bindings to access remote resources configured * with `remote: true` in the wrangler configuration file. */ remoteBindings: z.ZodDefault<z.ZodBoolean>; /** * Additional exports. * A map of module exports to be made available on the `ctx.exports` * that cannot be automatically inferred by analyzing the Worker source code. * * This is useful for exports that are re-exported implicitly, for example * through wildcard (`export * from "..."`) re-exports from virtual modules. */ additionalExports: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodLiteral<"WorkerEntrypoint">, z.ZodLiteral<"DurableObject">, z.ZodLiteral<"WorkflowEntrypoint">]>>>; miniflare: z.ZodOptional<z.ZodObject<{ workers: z.ZodOptional<z.ZodArray<z.ZodObject<{}, "passthrough", z.ZodTypeAny, z.objectOutputType<{}, z.ZodTypeAny, "passthrough">, z.objectInputType<{}, z.ZodTypeAny, "passthrough">>, "many">>; }, "passthrough", z.ZodTypeAny, z.objectOutputType<{ workers: z.ZodOptional<z.ZodArray<z.ZodObject<{}, "passthrough", z.ZodTypeAny, z.objectOutputType<{}, z.ZodTypeAny, "passthrough">, z.objectInputType<{}, z.ZodTypeAny, "passthrough">>, "many">>; }, z.ZodTypeAny, "passthrough">, z.objectInputType<{ workers: z.ZodOptional<z.ZodArray<z.ZodObject<{}, "passthrough", z.ZodTypeAny, z.objectOutputType<{}, z.ZodTypeAny, "passthrough">, z.objectInputType<{}, z.ZodTypeAny, "passthrough">>, "many">>; }, z.ZodTypeAny, "passthrough">>>; wrangler: z.ZodOptional<z.ZodObject<{ configPath: z.ZodOptional<z.ZodString>; environment: z.ZodOptional<z.ZodString>; }, "strip", z.ZodTypeAny, { environment?: string | undefined; configPath?: string | undefined; }, { environment?: string | undefined; configPath?: string | undefined; }>>; }, "strip", z.ZodTypeAny, { remoteBindings: boolean; additionalExports: Record<string, "WorkerEntrypoint" | "DurableObject" | "WorkflowEntrypoint">; main?: string | undefined; miniflare?: z.objectOutputType<{ workers: z.ZodOptional<z.ZodArray<z.ZodObject<{}, "passthrough", z.ZodTypeAny, z.objectOutputType<{}, z.ZodTypeAny, "passthrough">, z.objectInputType<{}, z.ZodTypeAny, "passthrough">>, "many">>; }, z.ZodTypeAny, "passthrough"> | undefined; wrangler?: { environment?: string | undefined; configPath?: string | undefined; } | undefined; }, { main?: string | undefined; remoteBindings?: boolean | undefined; additionalExports?: Record<string, "WorkerEntrypoint" | "DurableObject" | "WorkflowEntrypoint"> | undefined; miniflare?: z.objectInputType<{ workers: z.ZodOptional<z.ZodArray<z.ZodObject<{}, "passthrough", z.ZodTypeAny, z.objectOutputType<{}, z.ZodTypeAny, "passthrough">, z.objectInputType<{}, z.ZodTypeAny, "passthrough">>, "many">>; }, z.ZodTypeAny, "passthrough"> | undefined; wrangler?: { environment?: string | undefined; configPath?: string | undefined; } | undefined; }>; type SourcelessWorkerOptions = Omit<WorkerOptions, "script" | "scriptPath" | "modules" | "modulesRoot"> & { modulesRules?: ModuleRule[]; }; type WorkersPoolOptions = z.input<typeof WorkersPoolOptionsSchema> & { miniflare?: SourcelessWorkerOptions & { workers?: WorkerOptions[]; }; }; type WorkersPoolOptionsWithDefines = WorkersPoolOptions & { defines?: Record<string, string>; }; //#endregion //#region src/pool/plugin.d.ts interface WorkerPoolOptionsContext { inject: typeof inject; } declare function cloudflareTest(options: WorkersPoolOptions | ((ctx: WorkerPoolOptionsContext) => Promise<WorkersPoolOptions> | WorkersPoolOptions)): Vite.Plugin; //#endregion //#region src/pool/pool.d.ts declare function cloudflarePool(poolOptions: WorkersPoolOptions | ((ctx: WorkerPoolOptionsContext) => Promise<WorkersPoolOptions> | WorkersPoolOptions)): PoolRunnerInitializer; //#endregion //#region src/shared/d1.d.ts type D1Migration = { name: string; queries: string[]; }; //#endregion //#region src/pool/d1.d.ts /** * Reads all migrations in `migrationsPath`, ordered by migration number. * Each migration will have its contents split into an array of SQL queries. */ declare function readD1Migrations(migrationsPath: string): Promise<D1Migration[]>; //#endregion //#region src/pool/pages.d.ts declare function poolWorkerStarted(): void; declare function poolWorkerStopped(): void; declare function buildPagesASSETSBinding(assetsPath: string): Promise<(request: Request) => Promise<Response>>; //#endregion //#region src/pool/index.d.ts declare function structuredSerializableStringify(value: unknown): string; declare function structuredSerializableParse(value: string): unknown; declare function getRunnerName(project: TestProject, testFile?: string): string; interface DurableObjectDesignator { className: string; scriptName?: string; unsafeUniqueKey?: string; } /** * Returns a map of Durable Objects bindings' bound names to the designators of * the objects they point to. */ declare function getDurableObjectDesignators(options: WorkersPoolOptions): Map<string, DurableObjectDesignator>; declare function getProjectMiniflare(ctx: Vitest, project: TestProject, poolOptions: WorkersPoolOptionsWithDefines, main: string | undefined): Promise<Miniflare>; declare function maybeGetResolvedMainPath(project: TestProject, options: WorkersPoolOptionsWithDefines): string | undefined; declare function connectToMiniflareSocket(mf: Miniflare, workerName: string): Promise<miniflare0.WebSocket>; declare function assertCompatibleVitestVersion(ctx: Vitest): void; //#endregion export { type D1Migration, assertCompatibleVitestVersion, buildPagesASSETSBinding, cloudflarePool, cloudflareTest, connectToMiniflareSocket, getDurableObjectDesignators, getProjectMiniflare, getRunnerName, maybeGetResolvedMainPath, poolWorkerStarted, poolWorkerStopped, readD1Migrations, structuredSerializableParse, structuredSerializableStringify }; //# sourceMappingURL=index.d.mts.map