start-testing-cloudflare
v1.0.0
Published
Start Testing Cloudflare is a lightweight test harness for testing Cloudflare Workers and Durable Objects
Downloads
6
Maintainers
Readme
Start Testing Cloudflare
Start Testing Cloudflare is a lightweight test harness for testing Cloudflare Workers and Durable Objects using the minimal Start Testing library.
How it Works
Start Testing Cloudflare extends testing.Context
(see Start Testing) to create two new Runner
classes.
- The
CloudflareRunner
class implements Cloudlare's Fetcher interface and gets deployed as a Cloudflare Worker. - The
LocalRunner
class triggers individual top level tests via an HTTP post request to theCloudflareRunner
worker.- The Response is a serialized
CloudflareContext
representing the test result. - The
LocalRunner
deserializes the response and maps it to aLocalContext
used for local logging and reporting.
- The Response is a serialized
Example
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 })
Run
node --loader ts-node/esm ./scripts/runTests.ts