Part 3: Testing
Test the real thing: deploy the Stack, call the Worker, and destroy everything.
Install the test harness
Section titled “Install the test harness”pnpm add -D https://pkg.alchemy.run/@alchemy.run/test/pr:3743:56b4873bun add -d https://pkg.alchemy.run/@alchemy.run/test/pr:3743:56b4873npm install -D https://pkg.alchemy.run/@alchemy.run/test/pr:3743:56b4873yarn add -D https://pkg.alchemy.run/@alchemy.run/test/pr:3743:56b4873Write the test
Section titled “Write the test”import * as Cloudflare from "@alchemy.run/cloudflare";import * as Convex from "@alchemy.run/convex";import * as Test from "@alchemy.run/test/Bun";import * as Alchemy from "alchemy";import { expect } from "bun:test";import * as Effect from "effect/Effect";import * as HttpClient from "effect/http/HttpClient";import * as HttpClientRequest from "effect/http/HttpClientRequest";import * as Layer from "effect/Layer";import Stack from "../alchemy.run.ts";
const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ providers: Layer.mergeAll(Cloudflare.providers(), Convex.providers()), state: Alchemy.localState(),});
const stack = beforeAll(deploy(Stack));
afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
test( "a message written through the Worker reads back", Effect.gen(function* () { const { url } = yield* stack; const ready = yield* Test.getWhenReady(url); expect(ready.status).toBe(200);
const response = yield* HttpClient.execute( HttpClientRequest.post(url).pipe(HttpClientRequest.bodyText("hello")), ); const messages = (yield* response.json) as Array<{ body: string }>; expect(messages.map((message) => message.body)).toContain("hello"); }), { timeout: 120_000 },);beforeAll(deploy(Stack)) deploys once for the whole file, and afterAll destroys it.
Run it
Section titled “Run it”bun testbun testbun testbun testSet NO_DESTROY=1 to keep the deployment while you work on the test.
Run against a local Worker
Section titled “Run against a local Worker”Add dev: true to Test.make. The Worker runs on your machine, and Convex stays real:
const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ providers: Layer.mergeAll(Cloudflare.providers(), Convex.providers()), state: Alchemy.localState(), dev: true,});