Modern QA2026Git Hooks for Test Quality — tiles
Log inJoin
56 / 57 · 17 Git & Version Control · Gitignore and Practical Tips← prev⊞ allnext →☰ Read as one page

7.8Git Hooks for Test Quality

Git hooks run scripts automatically at specific points in the Git workflow. They can enforce quality standards locally.

# .git/hooks/pre-commit (or use husky for team-wide hooks)
#!/bin/sh

# Prevent committing .env files
if git diff --cached --name-only | grep -q '\.env'; then
  echo "ERROR: Attempting to commit .env file. Aborting."
  exit 1
fi

# Prevent committing test.only or describe.only
if git diff --cached | grep -q '\.only'; then
  echo "ERROR: Found .only in test files. Remove before committing."
  exit 1
fi

# Run lint on staged files
npx lint-staged

Using husky for team-wide hooks:

// package.json
{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },
  "lint-staged": {
    "tests/**/*.ts": ["eslint --fix", "prettier --write"]
  }
}