.opencode/skills/wd-test-format/reference/advanced-configs.md
.wd-test Config PatternsPatterns for Durable Objects, multi-service tests, network access, external services, and TypeScript tests.
To test Durable Objects, define the namespace and storage:
const unitTests :Workerd.Config = (
services = [
( name = "do-test",
worker = (
modules = [(name = "worker", esModule = embed "do-test.js")],
compatibilityDate = "2024-01-01",
durableObjectNamespaces = [
(className = "MyDurableObject", uniqueKey = "210bd0cbd803ef7883a1ee9d86cce06e"),
],
durableObjectStorage = (localDisk = "TEST_TMPDIR"),
bindings = [
(name = "MY_DO", durableObjectNamespace = "MyDurableObject"),
],
),
),
# Disk service for DO storage
(name = "TEST_TMPDIR", disk = (writable = true)),
],
);
The uniqueKey is a hex string that uniquely identifies the namespace. Use any 32-char hex string for tests.
For in-memory storage (no persistence between requests), use:
durableObjectStorage = (inMemory = void),
Tests can define multiple services that communicate via service bindings:
const unitTests :Workerd.Config = (
services = [
( name = "main-test",
worker = (
modules = [(name = "worker", esModule = embed "main-test.js")],
compatibilityDate = "2024-01-01",
bindings = [
(name = "BACKEND", service = "backend"),
],
),
),
( name = "backend",
worker = (
modules = [(name = "worker", esModule = embed "backend.js")],
compatibilityDate = "2024-01-01",
),
),
],
);
For large configs, factor out worker definitions as named constants:
const unitTests :Workerd.Config = (
services = [
(name = "main", worker = .mainWorker),
(name = "helper", worker = .helperWorker),
],
);
const mainWorker :Workerd.Worker = (
modules = [(name = "worker", esModule = embed "main.js")],
compatibilityDate = "2024-01-01",
bindings = [(name = "HELPER", service = "helper")],
);
const helperWorker :Workerd.Worker = (
modules = [(name = "worker", esModule = embed "helper.js")],
compatibilityDate = "2024-01-01",
);
For tests that need outbound network access:
( name = "internet",
network = (
allow = ["private"],
tlsOptions = (
trustedCertificates = [
embed "test-cert.pem",
],
),
)
),
allow can be ["private"] (loopback/LAN) or ["public"] (internet). Most tests use "private".
For testing RPC over sockets or external service communication:
( name = "my-external",
external = (
address = "loopback:my-external",
http = (capnpConnectHost = "cappy")
)
),
TypeScript test files use a .ts-wd-test extension for the config, but the embedded module is the compiled .js output:
Config file (my-test.ts-wd-test):
using Workerd = import "/workerd/workerd.capnp";
const unitTests :Workerd.Config = (
services = [(
name = "my-test",
worker = (
modules = [(name = "worker", esModule = embed "my-test.js")],
compatibilityDate = "2024-01-01",
),
)],
);
BUILD.bazel references the .ts source:
wd_test(
src = "my-test.ts-wd-test",
args = ["--experimental"],
data = ["my-test.ts"],
)
The build system compiles the .ts to .js automatically.