import request from 'supertest'; import app from '../../src/app'; import { pool } from '../../src/db'; import { resetDb, closeDb } from './setup/testDb'; beforeEach(async () => { await resetDb(); }); afterAll(async () => { await pool.end(); await closeDb(); }); describe('unexpected route failures', () => { it('answers with 500 instead of leaving the request hanging', async () => { // A non-numeric id reaches Postgres as `WHERE i.id = 'not-a-number'`, which // raises invalid-input-syntax. Express 4 does not forward a rejected async // handler on its own, so without the asyncRoute wrapper plus the error // middleware this request never gets a response at all — and a hung request // renders as an empty storefront rather than a visible failure. const res = await request(app).get('/api/items/not-a-number'); expect(res.status).toBe(500); expect(res.body.error).toBe('internal error'); }); it('does not leak the underlying database error to the client', async () => { const res = await request(app).get('/api/items/not-a-number'); expect(JSON.stringify(res.body)).not.toContain('syntax'); expect(JSON.stringify(res.body)).not.toContain('items'); }); });