การ run ทดสอบด้วย jest มี 2 วิธี คือ
Running a single test file
Jest จะค้นหาไฟล์ที่มีชื่อลงท้ายด้วย .test.js|.ts หรือ .spec.js|.ts ตามค่าเริ่มต้นที่เราได้กำหนดไว้ ดังนั้นเราสามารถรันไฟล์ทดสอบเฉพาะไฟล์ได้โดยการระบุชื่อ (หรือรูปแบบ) เป็นอาร์กิวเมนต์ให้กับคำสั่ง jest
jest myFile.test.js
# Run the file 'myFile.test.js'
เราสามารถระบุ path ได้เหมือนกัน
jest path/to/anotherFile.spec.js
Run the file at the specified path
run ทั้งหมดที่อยู่ใน directory
jest components/**/*.test.js
Run all test files inside the 'components' directory (using a pattern)
Using --findRelatedTests
option
ตัวเลือกนี้มีประโยชน์ เมื่อเราต้องการเรียกใช้ไม่เฉพาะไฟล์ที่ระบุ แต่ยังรวมถึงไฟล์ทดสอบที่นำเข้าด้วย
jest --findRelatedTests myFile.test.js
Note:
- เราอาจเลือกติดตั้ง Jest แบบ global ได้โดยใช้
npm install -g jest-cli
หากยังไม่ได้ติดตั้งไว้ก่อน - สำหรับ projects ที่ติดตั้ง Jest เอาไว้ใน dev dependency แล้ว เราสามารถใช้คำสั่ง
npx jest path/to/your/file.spec.js
.