Trung BìnhGit iconGit

pre-push hook chạy tests: trade-offs và cách configure để không làm chậm developer workflow?

pre-push hook chạy trước khi git push — có thể block push nếu tests fail.

Trade-off chính:
- Bảo vệ remote branch khỏi broken code
- Nhưng full test suite có thể mất 5-10 phút → developers làm theo cách khác (push thẳng không qua hook)

Setup thực tế — chỉ chạy tests liên quan:

bash
# .husky/pre-push:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# Lấy danh sách files đang thay đổi so với main:
CHANGED=$(git diff --name-only origin/main HEAD | grep -E "\.(ts|tsx)$")

if [ -n "$CHANGED" ]; then
  # Chỉ chạy tests của files bị thay đổi:
  npx jest --findRelatedTests $CHANGED --passWithNoTests
fi

Hoặc chạy fast subset:

bash
npm run test:unit  # unit tests only (~30s), không integration tests

Pattern phổ biến: pre-commit → lint/format (fast), pre-push → unit tests (medium), CI → full test suite (slow).

Client hooks vs Server hooks:
- Client hooks: developer có thể bypass (--no-verify)
- Server hooks (GitHub Actions): không thể bypass, bắt buộc
- Recommendation: pre-push là safety net cho developer bản thân, CI là enforcement thực sự

Bypass khi cần:

bash
git push --no-verify  # skip pre-push hook

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

Mở danh sách Git