Supertest · สร้าง E2E Test Harness
ติดตั้ง Supertest
เตรียม package และ type definitions ที่จำเป็นสำหรับ request test
ติดตั้ง package ที่ใช้ใน test
Supertest เป็นเครื่องมือที่ใช้เฉพาะตอนพัฒนาและตรวจระบบ จึงติดตั้งเป็น development dependency ส่วน `@types/supertest` ช่วยให้ TypeScript รู้จักรูปแบบของ request และ response ที่ library เปิดให้ใช้
แยก dependency ของ application กับ test
| กลุ่ม | ตัวอย่าง | เหตุผล |
|---|---|---|
| dependencies | @nestjs/common | จำเป็นตอน application ทำงาน |
| devDependencies | supertest, @types/supertest | ใช้ตอนรัน test และไม่ใช่ runtime API |
อย่าแก้ `node_modules` โดยตรงและอย่าซ่อน package ไว้นอก `package.json` เพราะทีมและ CI ต้องติดตั้ง dependency จาก manifest และ lockfile เดียวกัน
กฎสำคัญ
ใช้ version จาก lockfile
commit manifest และ lockfile ตาม convention ของ project เพื่อให้ local กับ CI ใช้ dependency ชุดเดียวกัน
type package ไม่ใช่ test runner
@types/supertest เพิ่มข้อมูลให้ TypeScript เท่านั้น การรัน test ยังต้องใช้ Jest หรือ runner ของ project