Trung BìnhTesting iconTesting

Vitest là gì? Tại sao là lựa chọn tốt cho Vite projects?

Vitest là test framework xây dựng trên Vite với Jest-compatible API — migrate từ Jest thường chỉ cần đổi import và config.

  • Lợi ích: reuse Vite config và transforms (cùng aliases, plugins, TypeScript setup) — không cần config riêng cho tests; nhanh hơn Jest nhờ esbuild transpilation và native ESM.
  • Watch mode thông minh: chỉ re-run tests liên quan đến changed files, smart dependency tracking qua Vite HMR graph.
  • In-source testing: viết tests trực tiếp trong source file trong if (import.meta.vitest) block — tests bị tree-shaken khỏi production build, hữu ích cho utility functions.
  • Browser mode: chạy tests trong real browser (Chromium/Firefox) thay vì jsdom — accurate DOM behavior, dùng cho component tests.
  • Concurrent tests: test.concurrent() chạy tests trong describe block song song, tăng speed.
  • Snapshot: tương thích với Jest snapshots, có inline snapshots.
  • Coverage: V8 provider (nhanh hơn) hoặc Istanbul. @vitest/ui: browser-based test runner UI để visualize và debug tests.
  • Khác biệt với Jest: Vitest dùng ES modules natively (không transform), config trong vitest.config.ts thay vì jest.config.js, một số globals như __dirname cần config thêm.

Xem toàn bộ Testing cùng filter theo level & chủ đề con.

Mở danh sách Testing