The simplest API testing kit.
Most testing frameworks are complicated, old, not ESM, etc. I just wanted something super simple that is easy to get started, lets you do anything you want without jumping through hoops, and gets out of your way.
npm install --save-dev treeder/testkitThen write simple tests like this:
import { assert } from 'testkit'
export async function test1(c) {
let r = await c.api.fetch(`/`)
console.log('r:', r)
assert(r.message == 'hello world!', 'not hello world!')
}Then add tests to TestKit and run:
import { TestKit } from 'testkit'
import { test1 } from './test1.js'
// create context:
let c = {
env: process.env,
}
// create TestKit
let testKit = new TestKit(c, [test1])
// run
await testKit.run()See full example with auth headers and what not that you can copy and paste at test/test.js
In your package.json, make sure you have 3 scripts to run:
run- to run your app.test:run- to run your tests, assumes that your app is already running.test- this starts testkit which will run app with theruncommand then once it is running,test:runwill execute to run your tests.
"scripts": {
"run": "npx -y wrangler dev",
"test:run": "node tests/tests.js",
"test": "npx -y treeder/testkit --port=8787",
},Then run:
npm testAdd npm test to your CI and if it passes, you're good. If it fails, don't merge!
Tests run in the order you define them in the array you pass to new TestKit().
If your test returns an object, that object will be merged into the context under a data field that you can access in subsequent tests.
Test 1:
export async function test1(c) {
let myObject = { name: 'john' }
return { myObject }
}That return data will be available in any subsequest test.
export async function test2(c) {
console.log(c.data.myObject)
}Clone the repo and run:
npm run testkit