Skip to content
 
 

Repository files navigation

testkit

The simplest API testing kit.

Motivation

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.

Getting started

npm install --save-dev treeder/testkit

Then 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

Running tests

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 the run command then once it is running, test:run will 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 test

Add npm test to your CI and if it passes, you're good. If it fails, don't merge!

Passing data through the tests

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)
}

To run examples in this repository.

Clone the repo and run:

npm run testkit

About

The simplest API testing kit.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages