Goal
ใช้ Vitest สำหรับ unit testing ด้วย Vite-native integration, watch mode, Jest-compatible API, และ Browser Mode
Scope
ใช้สำหรับ unit testing ใน Vite projects, TypeScript projects, และ projects ที่ต้องการ Jest-compatible API หรือ Browser Mode
Execute
1. Install Vitest
ติดตั้ง Vitest ด้วย bun
bun add -D vitest
ต้องการ Vite >=v6.0.0 และ Node >=v20.0.0
2. Configure Vitest
ตั้งค่า vitest.config.ts หรือใช้ vite.config.ts ตาม project requirements
3. Learn Fundamentals
เรียนรู้พื้นฐาน Vitest
- อ่าน
guide/quick-start.mdสำหรับเริ่มต้น - อ่าน
guide/configuration.mdสำหรับการตั้งค่า - อ่าน
guide/features.mdสำหรับ features หลัก
4. Understand Key Concepts
ทำความเข้าใจ key concepts
- อ่าน
key-concepts/test-suite.mdสำหรับ test suite - อ่าน
key-concepts/hooks.mdสำหรับ test hooks - อ่าน
key-concepts/matchers.mdสำหรับ matchers - อ่าน
key-concepts/fixtures.mdสำหรับ fixtures - อ่าน
key-concepts/test-context.mdสำหรับ test context
5. Learn Advanced Features
ศึกษา features ขั้นสูง
- อ่าน
guide/browser-mode.mdสำหรับ Browser Mode - อ่าน
guide/coverage.mdสำหรับ code coverage - อ่าน
guide/mocking.mdสำหรับ mocking - อ่าน
guide/benchmarking.mdสำหรับ benchmarking - อ่าน
guide/in-source-testing.mdสำหรับ in-source testing
6. Use Best Practices
ใช้ best practices ในการเขียน tests
- อ่าน
principles/arrange-act-assert.mdสำหรับ AAA pattern - อ่าน
principles/test-isolation.mdสำหรับ test isolation - อ่าน
principles/test-naming.mdสำหรับ test naming
7. Write Tests
เขียน tests ด้วย Vitest
- อ่าน
workflows/write-test.mdสำหรับเขียน tests - อ่าน
workflows/setup-vitest.mdสำหรับ setup
8. Run Tests
รัน tests ด้วย Vitest
bun run test
9. Migrate from Other Frameworks
Migration จาก test frameworks อื่น
- อ่าน
guide/migration.mdสำหรับ migration
Rules
- ใช้
bun run testสำหรับรัน tests (ไม่ใช้bun testเพราะจะรัน Bun test runner) - ใช้
describeสำหรับ grouping tests - ใช้
itหรือtestสำหรับ individual tests - ใช้
expectสำหรับ assertions (Jest-compatible) - ใช้
vi.mockสำหรับ mocking modules - ใช้
vi.fnและvi.spyOnสำหรับ mocking functions - ใช้
beforeEachและafterEachสำหรับ setup/teardown - ใช้ AAA pattern (Arrange-Act-Assert) สำหรับ test structure
- ใช้ descriptive test names
- ใช้ test isolation สำหรับ independent tests
- ใช้ TypeScript สำหรับ type-safe tests
- ใช้
test.extendสำหรับ fixtures และ test context - ใช้ tags สำหรับ test categorization และ filtering
- ใช้ Browser Mode สำหรับ component testing ใน real browser
- ใช้
vi.stubEnvและvi.stubGlobalสำหรับ environment mocking - ใช้ coverage (v8 หรือ istanbul) สำหรับ code coverage
- ใช้
vitest.config.tsหรือvite.config.tsสำหรับ configuration - ใช้ chainable modifiers (.skip, .only, .todo, .concurrent, .each, .runIf, .skipIf) สำหรับ test execution control
- ใช้
onTestFailedและonTestFinishedสำหรับ fine-grained cleanup - ใช้
expect.soft()สำหรับ soft assertions - ใช้
expect.poll()สำหรับ polling assertions - ใช้
expectTypeOfและassertTypeสำหรับ type-level assertions - ใช้
vi.hoisted()สำหรับ hoisting variables - ใช้
__mocks__folders สำหรับ automatic mocking - ใช้
--uiflag สำหรับ Vitest UI - ใช้
bench()สำหรับ benchmarking tests
Expected Outcome
- Tests ที่เขียนด้วย Vitest ตาม best practices
- Test coverage ที่ครอบคลุม
- Mocking ที่ถูกต้อง
- Performance ที่ดีด้วย parallel execution
- Tests ที่ maintainable และ readable
- Browser Mode สำหรับ component testing
- Test Context และ fixtures สำหรับ reusable test logic
- Tags สำหรับ test categorization และ filtering
- Benchmarking tests สำหรับ performance measurement
- In-source testing สำหรับ tests ที่อยู่ใกล้ implementation
- Type-level assertions สำหรับ type safety
- Soft assertions และ polling assertions สำหรับ advanced testing scenarios