Skip to content

Fail the contract test when a route is not documented - #6

Merged
vadim9999 merged 1 commit into
mainfrom
test/undocumented-routes
Nov 23, 2026
Merged

vadim9999 merged 1 commit into
mainfrom
test/undocumented-routes

Conversation

@vadim9999

Copy link
Copy Markdown
Contributor

What

  • A new test in test/openapi.test.ts lists every route of the app (GET /health, PATCH /tree/nodes/{key}, …) and fails if one is missing from buildOpenApiDocument(). The failure names the route, e.g. expected [ 'POST /tree/nodes/{key}/duplicate' ] to deeply equal [].
  • AGENTS.md and the add-endpoint skill describe what the contract test now catches.

Why

The contract test only caught documented routes that the app does not have. The reverse was not checked: a new endpoint that was left out of src/openapi.ts passed CI. It then never reached openapi.json, and the frontend (which now generates its types from that file, reactjs #53) got no types for it.

How it works

Express 5 does not keep a router's mount path (/tree) on the router. So the test spies on Router.prototype.use, which records which router was mounted where and still calls the real method. The routes themselves are read from router.stack / layer.route.

Those are Express internals, unchanged since Express 4. If a future Express upgrade changes them, the test does not silently pass on an empty list: it also requires PATCH /tree/nodes/{key} to be listed.

How to verify

  • npm test: 56 tests.
  • Add a route without documenting it, e.g. router.post("/nodes/:key/duplicate", controller.addNode) in tree.router.ts, then run npx vitest run test/openapi.test.ts. The test fails and names that route. All 12 current routes are listed and documented.

Checklist

  • One concern per PR
  • npm run lint, npm run typecheck, npm run format:check pass
  • npm test passes; new behavior has a test
  • Routes or schemas changed → npm run openapi run, openapi.json and the README endpoint table updated (not applicable: no route changed)
  • Breaking API change → matching PR in email-template-editor-reactjs linked (not applicable)

test/openapi.test.ts caught documented routes the app does not have,
but not the reverse: a new endpoint left out of buildOpenApiDocument()
passed CI, and the frontend got no types for it.

The new test lists every route of the app and checks that each one is
in the contract. Express 5 does not keep a router's mount path, so a
spy on Router.prototype.use records where each router was mounted; the
routes come from router.stack and layer.route. Those are Express
internals, so the test also requires a known route to be listed: if an
upgrade changes them, it fails instead of passing on an empty list.
@vadim9999
vadim9999 merged commit 0ab173b into main Nov 23, 2026
2 checks passed
@vadim9999
vadim9999 deleted the test/undocumented-routes branch November 23, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant