Husky - GradedJestRisk/js-training GitHub Wiki

General

Husky:

  • plugs on git hooks
  • is configured through package.json, which is easier to share than .git/hooks/* file
  • handle subdirectories in single repository (see here)
Note: there is no pre-staging hook (full list here)

install

Steps:

  • create a git repository git init
  • install module: npm install husky --save-dev
  • check it has output husky > Setting up git hooks
    • if not, look at .git/hooks/pre-commit, it should contain . "$(dirname "$0")/husky.sh"
    • if not, then run HUSKY_DEBUG=1 npm install husky --save-dev and look in husky issues
  • setup a failing hook in package.json
  "devDependencies": {
    "husky": "^4.3.0",
  },
  "husky": {
    "hooks": {
      "pre-commit": "echo \"this should fail\" && exit 1"
    }
  }
  • create an empty file touch foo.bar
  • add to staging git add foo.bar
  • try to commit git commit --allow-empty
  • it should fail with message
husky > pre-commit (node v10.15.3)
this should fail
husky > pre-commit hook failed (add --no-verify to bypass)

configure

Add hook on commit for test, add after scripts in package.json

  "husky": {
    "hooks": {
      "pre-commit": "npm test",
      "pre-push": "npm test"
    }
  }

Hook for test and lint: "pre-commit": "npm test && npm run lint"

⚠️ **GitHub.com Fallback** ⚠️