Get test coverage on an Express/Node API fast — the first 5 tests that catch
94
90%
Does it follow best practices?
Impact
100%
1.26xAverage score across 5 eval scenarios
Passed
No known issues
A hotel booking API built with Express and TypeScript has been in production for several months. The front-end team has complained that error handling is inconsistent: some endpoints return { message: "not found" }, others return { error: "Bad request" }, and at least one returns an HTML error page when given unexpected input. This makes it very difficult to display useful error messages to users, since the client code can't rely on a predictable error shape.
The back-end team has agreed to standardize all error responses. Before they start refactoring, they want a test that documents the current behavior and will serve as a regression test once the fix is in place — verifying that all error conditions return errors in the same shape.
Write a test suite in __tests__/error-format.test.ts that:
Also produce a short error-format-spec.md describing the error shape the tests enforce.
The API has these endpoints:
POST /api/bookings — requires guest_name (string) and check_in (ISO date string); returns 400 on invalid inputGET /api/bookings/:id — returns 404 when booking not foundDELETE /api/bookings/:id — returns 404 when booking not foundThe following files are provided as inputs. Extract them before beginning.
=============== FILE: src/server.ts =============== import express from 'express'; import { bookingsRouter } from './routes/bookings'; import { errorHandler } from './middleware/error-handler';
export const app = express(); app.use(express.json()); app.use('/api/bookings', bookingsRouter); app.use(errorHandler);
if (process.env.NODE_ENV !== 'test') { app.listen(3000); }
=============== FILE: package.json =============== { "name": "booking-api", "version": "1.0.0", "scripts": { "test": "vitest run", "test:watch": "vitest" }, "devDependencies": { "vitest": "^1.2.0", "supertest": "^6.3.4", "@types/supertest": "^6.0.2" } }