Skip to content
workerstackPublic

About

Testing for APIs, web apps and Workers: /api tests a live HTTP API against its OpenAPI document. npm i -D @workerstack/testing

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

587 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@workerstack/testing

Testing for APIs, web apps and Workers: /api tests a live HTTP API against its OpenAPI document; /web guards, readies, scans and network-gates Playwright tests

Contents

Overview

npm

Testing for APIs, web apps and Workers. It ships /api, the API tester: live matrix testing of a running backend against its own OpenAPI document. And /web: Playwright tests that fail on a page error and wait on the page, not the clock. The API tester reads the spec, talks to the API, and treats every way to see a record as a cell in a matrix: list, item, filter, sort, page, cursor, select, search, parent routes, another tenant. Then it checks that the cells agree. It never reads your source or assumes your framework.

A schema fuzzer asks did this answer match its schema? The API tester asks that of every request it sends, and then the questions only state can answer:

  • the row is on GET /tables/{id} and missing from GET /tables
  • ?filter=status.eq.nope returns every row (the backend dropped the parameter)
  • limit=2 walks to 9 rows and limit=100 to 10 (the sort has no total order)
  • PATCH { name } also cleared description
  • ?filter=id.eq.<another tenant's id> returns that tenant's row

There is no ground-truth database. A filter and its negation must partition the set, a page walk must cover the collection, and a record read four ways must read the same. One root cause is one finding; the checks that depend on it are BLOCKED, not a page of copies.

Getting started

Install

npm i -D @workerstack/testing

Requires Node.js 22.18 or later. The API tester reads OpenAPI 3.0–3.2 documents, in JSON or YAML, and runs as testing api. Renamed: the migration guide maps every name.

Quick start

testing api doctor --spec https://api.example.com/openapi.json   # offline: what the API tester can test, and what is missing
testing api init --spec https://api.example.com/openapi.json     # writes testing.api.config.ts from the security schemes
testing api run --config testing.api.config.ts                            # tests the running API

doctor names every coverage gap and the tag that would close it. To see the API tester find bugs first, run the bundled demo API with planted defects (testing api serve --defects STALE_LIST,PATCH_REPLACES) and point labs/api/local.config.ts at the URL it prints.

Authentication

Each operation is sent the credentials its OpenAPI security requires, placed where its scheme says. The config only says how to get each scheme's value:

// testing.api.config.ts
import { defineConfig } from "@workerstack/testing/api"

export default defineConfig({
	spec: "https://api.example.com/openapi.json",
	baseUrl: "https://api.example.com",
	security: { bearer: { static: "${API_TOKEN}" } },
	principals: {
		alpha: { tenant: "a" },
		beta: { tenant: "b", security: { bearer: { static: "${API_TOKEN_B}" } } },
	},
})
recipe the value
static a fixed token or key, or { username, password } for basic auth
login steps the API tester runs (a sign-in), then where the credential is in the answer
mint an operation called with another scheme's credential (an API key)
oauth2 a clientCredentials or password grant at the document's token URL
hook hooks.resolveCredential returns it

A second principal in another tenant is what makes the isolation checks run.

Verdicts and exit codes

verdict meaning
BACKEND_BUG the handler is wrong
SPEC_BUG the document is wrong, or disagrees with the handler
SECURITY an isolation or authorization failure across a declared boundary
AMBIGUITY the same evidence, where the boundary was only inferred
COVERAGE_GAP a check could not run; the report names the tag that would let it
BLOCKED a check this one depends on already failed
exit means
0 no defects
1 a root-cause finding (BACKEND_BUG, SPEC_BUG, SECURITY, AMBIGUITY)
2 usage: a missing or wrong flag, or a config the document contradicts
3 The API tester could not do what it was asked (the network never came back, say)
4 the run may have left records behind; they are listed under Leftovers

Any non-zero exit fails CI. Gaps and blocked checks never fail a full run on their own.

CI

- run: npm i -D @workerstack/testing
- run: npx testing api doctor --spec "$SPEC_URL"
- run: npx testing api run --config testing.api.config.ts
  env:
    API_TOKEN: ${{ secrets.API_TOKEN }}
    API_TOKEN_B: ${{ secrets.API_TOKEN_B }}

The report is written to .testing/api/runs/latest/report.json; read it to sort findings by verdict.

Limits

  • Bodies are generated from the document; the API tester checks HTTP statuses and JSON, never file contents.
  • A 429 is retried up to 5 times, honoring Retry-After up to network.maxRetryAfterMs (default 60s); a longer wait is not waited for, and the 429 stands.
  • Leftover rows on a shared database poison the numeric and filter checks: wipe them between runs.
  • Webhooks, callbacks and links are not followed, and URL $refs are not fetched.

The full list, with what you see and what to do instead, is in docs/api/limits.md.

Docs

License

MIT

Gates

Script contract

This repo has these package.json scripts. Hooks and CI call only these names and bun repo, so a tool change is one line in package.json. A script runs one tool; one that needs several names its sequence in scripts/steps.ts.

script runs
fmt / fmt:check repo fmt, with the standards pack's generated .oxfmtrc.jsonc
lint / lint:fix repo lint: oxlint with the generated .oxlintrc.jsonc, plus the repo's own rule plugin in lint/, registered in repo.config.ts
typecheck tsc --noEmit over each tsconfig.json (the root, src/web, tests/web) and each build config
test the build, then the unit and integration suites and the conformance self-test

Where each gate runs

The standards pack writes the hooks into .githooks/ and points core.hooksPath at it.

when what blocks
pre-commit repo precommit: format, lint under .repo/lint-baseline.json, typecheck, sync and the related tests of the staged files, then repo check the commit
commit-msg repo commit-msg (Conventional Commits) the commit
pre-push repo link --pre-push, then repo commit-msg --pre-push on every pushed commit the push
CI every push to main: the commits job runs repo commit-msg --range on the pushed commits; then fmt:check, repo check, typecheck, test, test:integration, test:e2e, test:web new work, until fixed

.repo/lint-baseline.json is the adoption ratchet: it holds each file's count of each rule the code broke when the standards pack came in. repo check fails a count above its entry, so a count only goes down; bun repo lint --baseline rewrites it after a fix.

tests/structure/workflows.test.ts checks the jobs in .github/workflows/ci.yml.

Push and CI

No full suite runs locally. Each commit runs the tests its change reaches: the touched test files and the tests of the code it changed. Then push, and run bun repo ci wait in the background. It waits for CI's run on the pushed commit and names any failed job and its log: read the log, fix, commit, push, and wait again. A push cancels the run before it, so batch the commits and push once.

Release

The version bump rides in the final push. Only a commit whose CI run passed on main gets the v* tag. The release workflow runs no tests: it waits for that commit's ci.yml push run and publishes on its success, so a bump made after the green push is a new commit and costs another CI run.

Browsers

  • Chrome: offscreen --hypr adds a hidden virtual monitor to the running Hyprland; tests drive Chrome on it over CDP. It never takes focus. exec and run are refused in that mode.
  • Safari: the MacBook Air M3 over Tailscale (ssh mac, safaridriver -p 4799), one run at a time.

Red, fix, green

  1. Red: write the test that shows the bug or the missing behavior. Run it. It must fail, for the reason the test names. Record the failing output (BUGS.md for a bug, where the repo has one).
  2. Fix: the smallest change that makes it pass.
  3. Green: run it again and record the passing output. Then run the tests the change reaches, and leave the full gates to CI on the push.

A test that cannot fail proves nothing. Check it: revert or break the fix, and the test must go red.

Commits: the test and the fix may share a commit or come as test(...) then fix(...). A fix or feat commit always contains a test change.

Lint rules follow the same loop. A mistake that a rule could catch mechanically ships with a rule: a RuleTester case that fails first, then the rule, in the repo's lint/ plugin.

About

Testing for APIs, web apps and Workers: /api tests a live HTTP API against its OpenAPI document. npm i -D @workerstack/testing

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages