El caso para tests pre-push
Un push roto desperdicia el tiempo de todos. Un hook pre-push ejecutando la suite de tests detecta el problema antes de que el push salga de tu máquina.
El hook pre-push más simple
# .git/hooks/pre-push
#!/usr/bin/env bash
set -e
npm test --silent
Leer lo que se está pusheando
#!/usr/bin/env bash
while read local_ref local_sha remote_ref remote_sha; do
if [ "$local_sha" = "0000000000000000000000000000000000000000" ]; then
continue
fi
git checkout "$local_sha" -- .
npm test
done
Ejecuciones selectivas de tests
changed=$(git diff --name-only origin/main...HEAD)
affected=$(./scripts/find-affected-tests.sh "$changed")
npx jest $affected
Aplicación consciente del branch
protected_branches=("main" "release")
branch=$(git symbolic-ref --short HEAD)
if [[ " ${protected_branches[*]} " =~ " $branch " ]]; then
echo "Rechazando push directo a $branch" >&2
exit 1
fi
Usar Husky para hooks compartidos
# .husky/pre-push
npm test --silent
npm run typecheck --silent
Tips de rendimiento
- Ejecutar tests en paralelo:
jest --maxWorkers=50%. - Saltar tests no cambiados:
jest --onlyChanged. - Cachear transformaciones.
- Reservar tests pesados de integración para CI.
Saltarse
git push --no-verify
Hooks del lado servidor
# /srv/git/repo.git/hooks/pre-receive
#!/usr/bin/env bash
while read old new ref; do
if ! gitleaks detect --redact --no-banner --staged 2>/dev/null; then
echo "Push rechazado: secretos detectados" >&2
exit 1
fi
done
El límite correcto
- Pre-commit: verificaciones instantáneas (lint, format, typecheck).
- Pre-push: tests unitarios, lint completo.
- CI: tests de integración, smoke tests, deploy preview.