start-testing-cloudflare
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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.

  1. The CloudflareRunner class implements Cloudlare's Fetcher interface and gets deployed as a Cloudflare Worker.
  2. The LocalRunner class triggers individual top level tests via an HTTP post request to the CloudflareRunner worker.
    • The Response is a serialized CloudflareContext representing the test result.
    • The LocalRunner deserializes the response and maps it to a LocalContext used for local logging and reporting.

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

/start-testing-cloudflare/

    Package Sidebar

    Install

    npm i start-testing-cloudflare

    Weekly Downloads

    0

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    17.4 kB

    Total Files

    30

    Last publish

    Collaborators

    • connormckelvey