Repository navigation
Fail the contract test when a route is not documented - #6
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
test/openapi.test.tslists every route of the app (GET /health,PATCH /tree/nodes/{key}, …) and fails if one is missing frombuildOpenApiDocument(). The failure names the route, e.g.expected [ 'POST /tree/nodes/{key}/duplicate' ] to deeply equal [].AGENTS.mdand theadd-endpointskill 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.tspassed CI. It then never reachedopenapi.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 onRouter.prototype.use, which records which router was mounted where and still calls the real method. The routes themselves are read fromrouter.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.router.post("/nodes/:key/duplicate", controller.addNode)intree.router.ts, then runnpx vitest run test/openapi.test.ts. The test fails and names that route. All 12 current routes are listed and documented.Checklist
npm run lint,npm run typecheck,npm run format:checkpassnpm testpasses; new behavior has a testnpm run openapirun,openapi.jsonand the README endpoint table updated (not applicable: no route changed)