Supertest · ตรวจ Response Contract
Stable Public Contract
แยก public fields ออกจาก internal fields เพื่อให้ test ทนต่อ refactor
ทำให้ test ทนต่อ refactor
Test ที่ดีเป็นรั้วของ behavior ไม่ใช่ snapshot ของ implementation ทุก field เมื่อ service หรือ database เปลี่ยนภายใน test ควรยังผ่านถ้า public response ยังมีความหมายเดิม
assert public fields และกัน internal leakTS
contract ที่ stable ต้องมีขอบเขต
กฎสำคัญ
public
กำหนด field ที่ client พึ่งพา
field เหล่านั้นควรมี assertion ที่อ่านแล้วเห็น business meaning
flexible
ปล่อย field ที่ไม่ใช่สัญญา
ไม่ต้อง assert metadata หรือ internal implementation เว้นแต่ requirement ระบุไว้
safe
assert absence เมื่อความปลอดภัยสำคัญ
เช่น password hash หรือ internalVersion ไม่ควรหลุดไปยัง response
ความ stable ไม่ได้แปลว่าใช้ matcher หลวมเสมอ หาก field สำคัญหายไป test ต้อง fail การเลือก partial assertion จึงต้องมาคู่กับรายการ public contract ที่ชัดเจน