Supertest · ตรวจ Response Contract
Error Response Shape
กำหนดสิ่งที่ควรตรวจใน error payload
Error response ก็เป็น public contract
เมื่อ request fail client ยังต้องรู้ว่าเกิดอะไรขึ้นและควรแก้อย่างไร ดังนั้น test error ควรตรวจ status และ shape ที่ caller ใช้ เช่น `statusCode`, `error` และ `message` โดยไม่ผูกกับข้อความภายในที่เปลี่ยนได้ง่ายเกินไป
แยก error ที่ caller แก้ได้กับ internal error
| error | status | สิ่งที่ test ควรยืนยัน |
|---|---|---|
| invalid input | 400 | message ช่วยแก้ payload ได้ |
| missing resource | 404 | client รู้ว่า resource ไม่มี |
| duplicate | 409 | client แยก conflict จาก validation ได้ |
| unexpected failure | 500 | ไม่ leak stack หรือ secret |
i
ข้อความ error ไม่ใช่ทั้งหมดของ contract
ถ้า message เป็น array จากหลาย validation rule ให้ตรวจชนิดและข้อความสำคัญที่ stable แทนการบังคับลำดับทุกตัว