reactjsgitconfighuskygit-husky

trying to use pre-commit hooks using husky not working


I'm been configuration husky for a while but i been encounter in this issue i think i have the right configuration but when i try to commit the hooks is not been call.

the below is my code base

package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.11.68",
    "@types/react": "^18.0.21",
    "@types/react-dom": "^18.0.6",
    "eslint-config-airbnb-typescript": "^17.0.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.8.4",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "eslint .",
    "lint:fix": "eslint --fix .",
    "prepare": "husky install"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.40.1",
    "@typescript-eslint/parser": "^5.40.1",
    "eslint": "^8.25.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-jsx-a11y": "^6.6.1",
    "eslint-plugin-prettier": "^4.2.1",
    "eslint-plugin-react": "^7.31.10",
    "eslint-plugin-react-hooks": "^4.6.0",
    "husky": "^8.0.0",
    "prettier": "^2.7.1"
  }
}

pre-commit file

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

npm test

i dont have any output so feeling weird about that my git version is

git version 2.37.0 (Apple Git-136)

Solution

  • To test whether some existing pre-commit hook is being run at all, modify it so that it exits with an error message, preventing the commit. In this case you could change the .git/hooks/pre-commit file to read:

    #!/usr/bin/env sh
    echo debugging the pre-commit hook 1>&2
    exit 1
    # . "$(dirname -- "$0")/_/husky.sh"
    
    # npm test
    

    If git commit prints the message and prevents you from committing, you know that the pre-commit hook is running. If the commit succeeds, you know that the pre-commit hook is not running.

    If the pre-commit hook is not running, check the usual suspects: see git pre- and post- commit hooks not running.