Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

5.9 KB
Raw Blame
---
title: "Part 3: Testing"
description: Write integration tests that deploy your stack and make HTTP requests against your live Lambda Function URL.
sidebar:
  order: 3
---

In [Part 2](/aws/tutorial/part-2) you deployed a Lambda with S3
bindings. Now you'll write integration tests that deploy the stack,
hit the live Function URL over HTTP, and verify it works.

## Create the test file

Alchemy ships test utilities for Bun that wrap `bun:test` with Effect
support. Configure providers (and state) once at the top of the file
with `Test.make({...})` — the same `AWS.providers()` / `AWS.state()`
you used in your `Stack`:

```typescript
// test/integ.test.ts
import * as AWS from "alchemy/AWS";
import * as Test from "alchemy/Test/Bun";
import { expect } from "bun:test";
import * as Effect from "effect/Effect";
import Stack from "../alchemy.run.ts";

const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
  providers: AWS.providers(),
  state: AWS.state(),
});
```

`expect` (and any other `bun:test` helpers like `describe`) come from
`bun:test` directly — `Test.make` only provides the Effect-aware test
runner pieces.

:::note[Using Vitest?]
`alchemy/Test/Vitest` exposes the exact same API — swap the import
and take `expect` from `@effect/vitest` instead. See
[Test harness → Bun vs Vitest](/testing/test-harness#bun-vs-vitest).
:::

## Deploy the stack before tests

Use `beforeAll` with `deploy` to deploy your stack once before any
tests run:

```diff lang="typescript"
const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
  providers: AWS.providers(),
  state: AWS.state(),
});

+const stack = beforeAll(deploy(Stack));
```

`deploy(Stack)` returns an Effect that plans and applies the stack.
`beforeAll` runs it once, then returns a lazy accessor you can
`yield*` inside each test to get the stack outputs.

## Access the stack outputs

Write your first test. Use `yield* stack` to get the outputs you
returned from your Stack in Part 2:

```diff lang="typescript"
const stack = beforeAll(deploy(Stack));

+test(
+  "stack exposes a function url",
+  Effect.gen(function* () {
+    const { url } = yield* stack;
+
+    expect(url).toBeString();
+  }),
+);
```

`test(name, effect)` wraps `bun:test` — you write an Effect generator
instead of an async function.

## Run the tests

```sh
bun test test/integ.test.ts
```

The first run deploys the stack (or reuses the existing one if
already deployed). Subsequent runs are fast because Alchemy diffs and
skips unchanged resources.

:::note[Which stage do tests deploy to?]
The test harness defaults to a stage named `test`, separate from the
`dev_<user>` stage your `alchemy deploy` runs target — so tests never
clobber your working deployment. More on stages in
[Part 4](/aws/tutorial/part-4).
:::

## Add HTTP assertions

The basic test just checks that a URL exists. Let's verify the
function actually handles requests:

```diff lang="typescript"
// test/integ.test.ts
import * as AWS from "alchemy/AWS";
import * as Test from "alchemy/Test/Bun";
import { expect } from "bun:test";
import * as Effect from "effect/Effect";
+import * as HttpBody from "effect/unstable/http/HttpBody";
+import * as HttpClient from "effect/unstable/http/HttpClient";
import Stack from "../alchemy.run.ts";

const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
  providers: AWS.providers(),
  state: AWS.state(),
});

const stack = beforeAll(deploy(Stack));

-test(
-  "stack exposes a function url",
-  Effect.gen(function* () {
-    const { url } = yield* stack;
-
-    expect(url).toBeString();
-  }),
-);
+test(
+  "PUT and GET round-trip an object",
+  Effect.gen(function* () {
+    const { url } = yield* stack;
+
+    const put = yield* HttpClient.put(`${url}/hello.txt`, {
+      body: HttpBody.text("Hello, World!"),
+    });
+    expect(put.status).toBe(204);
+
+    const get = yield* HttpClient.get(`${url}/hello.txt`);
+    expect(yield* get.text).toBe("Hello, World!");
+  }),
+);
+
+test(
+  "GET missing key returns 404",
+  Effect.gen(function* () {
+    const { url } = yield* stack;
+    const response = yield* HttpClient.get(`${url}/no-such-key`);
+    expect(response.status).toBe(404);
+  }),
+);
```

`HttpClient` is provided automatically by the test harness — no extra
setup needed.

:::tip[Cold starts]
A freshly created Function URL can take a few seconds before it
serves its first request — DNS propagation, IAM propagation, and the
function's own cold start all stack up. If the very first request in
a fresh stage flakes, retry it with a bounded schedule:

```typescript
import * as Schedule from "effect/Schedule";

const get = yield* HttpClient.get(`${url}/hello.txt`).pipe(
  Effect.retry({ schedule: Schedule.exponential("500 millis"), times: 10 }),
);
```
:::

## Destroy after tests on CI

Right now the stack stays deployed after tests finish. That's great
locally — you can re-run tests instantly against the already-deployed
stack. But on CI you want to clean up.

Add `afterAll` with `destroy`, using `skipIf` to only tear down when
`CI` is set:

```diff lang="typescript"
const stack = beforeAll(deploy(Stack));

+afterAll.skipIf(!process.env.CI)(destroy(Stack));

test(/* .. */);

test(/* .. */);
```

- **Locally** — `CI` is not set, so `skipIf` skips the destroy. You
  iterate fast against the live stack.
- **On CI** — set `CI=true` and the stack is torn down automatically
  after tests complete.

## Recap

You now have:

- `Test.make({ providers, state })` to wire your provider Layer and
  state store into the test runner once per file
- `beforeAll(deploy(Stack))` to deploy once before tests
- `yield* stack` to access outputs in each test
- HTTP assertions using Effect's `HttpClient`
- `afterAll.skipIf(!process.env.CI)(destroy(Stack))` for automatic
  cleanup on CI with fast iteration locally

In [Part 4](/aws/tutorial/part-4), you'll use stages to give every
developer, environment, and pull request its own isolated copy of the
stack.