Start Testing Cloudflare is a lightweight test harness for testing Cloudflare Workers and Durable Objects using the minimal Start Testing library.
Start Testing Cloudflare extends testing.Context (see Start Testing) to create two new Runner classes.
- The
CloudflareRunnerclass implements Cloudlare's Fetcher interface and gets deployed as a Cloudflare Worker. - The
LocalRunnerclass triggers individual top level tests via an HTTP post request to theCloudflareRunnerworker.- The Response is a serialized
CloudflareContextrepresenting the test result. - The
LocalRunnerdeserializes the response and maps it to aLocalContextused for local logging and reporting.
- The Response is a serialized
test/index.ts
import * as testing from 'start-testing'
import { CloudflareRunner, CloudflareContext } from 'start-testing-cloudflare/dist/cloudflare/index.js'
// Define your env interface for various resource bindings
interface Env {
FooObject: DurableObjectNamespace
}
const tests = {
testDurableObject: async (t: CloudflareContext<Env>) => {
const env = t.cf.env!
const id = env.FooObject.idFromName("test")
const obj = env.FooObject.get(id)
const res = await obj.fetch("/test")
if (res.status != 200) {
t.error(`got ${res.status}`)
}
}
}
export default new CloudflareRunner<Env>('cloudflare tests', tests)scripts/runTests.ts
import cloudflareRunner from '../test/index.js'
import { LocalRunner } from 'start-testing-cloudflare'
new LocalRunner('tests', cloudflareRunner).runSuite()
.then(process.exit)
.catch(e => { throw e })node --loader ts-node/esm ./scripts/runTests.ts