Supertest · คำขอ HTTP แรก
Request Target
สร้าง request target จาก HTTP server ที่ NestJS เปิดไว้
Request target คืออะไร
Request target คือสิ่งที่ Supertest ใช้เป็นปลายทางของ request ใน NestJS E2E เราใช้ `app.getHttpServer()` แทนการต่อ port เอง เพราะ target นี้เชื่อมกับ application ที่สร้างจาก TestingModule แล้ว
สร้าง target แล้วสร้าง requestTS
`request(server)` คืน request builder ที่ต่อ method เช่น `.get()` ได้
อย่าสับสน target, path และ response
| ส่วน | ความหมาย | ตัวอย่าง |
|---|---|---|
| target | server ที่รับ request | app.getHttpServer() |
| path | resource ที่ต้องการเรียก | /todos/7 |
| response | ผลลัพธ์จาก server | status, headers, body |
กฎสำคัญ
setup
ส่ง target ที่พร้อมใช้งาน
สร้าง app และเรียก `app.init()` ก่อนใช้ `getHttpServer()` ใน test
path
path ต้องตรง route contract
`/todos` กับ `/todo` เป็นคนละ path แม้ชื่อ resource คล้ายกัน