Supertest · ตรวจ Response Contract
Array และ Nested Response
ตรวจ collection และ object ที่ซ้อนกันโดยยังอ่านง่าย
ตรวจ array และ object ที่ซ้อนกัน
Collection response มักมี array อยู่ใน property เช่น `items` และอาจมี nested object เช่น `owner` หรือ `meta` ใช้ Jest asymmetric matchers เพื่อบอกเฉพาะ shape ที่ต้องพิสูจน์โดยไม่ทำให้ test อ่านยาก
ตรวจ collection และ nested itemTS
แยก concern ของ collection
| สิ่งที่ตรวจ | ตัวอย่าง assertion | ความหมาย |
|---|---|---|
| มี item ที่ต้องการ | arrayContaining | collection ไม่ได้หาย record สำคัญ |
| จำนวนรวม | total เป็น number หรือค่าที่คาด | pagination metadata ยังถูกต้อง |
| ลำดับ | toEqual กับ array ที่กำหนด | ใช้เมื่อ order เป็น public contract จริง |
กฎสำคัญ
precision
อย่าใช้ arrayContaining แทนทุกอย่าง
ถ้า contract รับรองจำนวนหรือ order ต้อง assert สองสิ่งนั้นเพิ่ม ไม่เช่นนั้น array ที่มีข้อมูลเกินก็ผ่านได้
shape
nested matcher ต้องตรงระดับ
วาง `objectContaining` ใน array matcher ให้ตรงกับโครงสร้าง response ที่อ่านจริง