An open API service indexing awesome lists of open source software.

https://github.com/treeder/testkit

The simplest API testing kit.
https://github.com/treeder/testkit

Last synced: 4 months ago
JSON representation

The simplest API testing kit.

Awesome Lists containing this project

README

          

# 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

```sh
npm install --save-dev treeder/testkit
```

Then write simple tests like this:

```js
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:

```js
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](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.

```json
"scripts": {
"run": "npx -y wrangler dev",
"test:run": "node tests/tests.js",
"test": "npx -y treeder/testkit --port=8787",
},
```

Then run:

```sh
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:

```js
export async function test1(c) {
let myObject = { name: 'john' }
return { myObject }
}
```

That return data will be available in any subsequest test.

```js
export async function test2(c) {
console.log(c.data.myObject)
}
```

## To run examples in this repository.

Clone the repo and run:

```sh
npm run testkit
```