huskygit-husky

Husky Hook does not work properly with Test


I'm configuring Husky to block commits if the prettier or eslint accuses both an error or warn, and block the push if it doesn't pass the tests.

However, when running the test, the test CLI is displayed (it is in the image below), with this, no key works, but I can only finish by clicking with the shortcut ctrl+z, which suspends my push.

MY CODE IN .HUSKY FOLDER:

pre-commit

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

yarn prettier-check
yarn lint-check

pre-push

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

yarn test

MY PACKAGE.JSON:

{
    "name": "web-whatsapp",
    "version": "0.0.0",
    "private": true,
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject",
        "prettier-check": "prettier --check 'src/**/*.{ts,tsx}'",
        "prettier-fix": "prettier --write 'src/**/*.{ts,tsx}'",
        "lint-check": "eslint 'src/**/*.{ts,tsx}'",
        "lint-fix": "eslint --fix 'src/**/*.{ts,tsx}'",
        "prepare": "husky install"
    },
    "dependencies": {
        "@testing-library/jest-dom": "^5.11.4",
        "@testing-library/react": "^11.1.0",
        "@testing-library/user-event": "^12.1.10",
        "@types/jest": "^26.0.15",
        "@types/node": "^12.0.0",
        "@types/react": "^17.0.0",
        "@types/react-dom": "^17.0.0",
        "react": "^17.0.2",
        "react-dom": "^17.0.2",
        "react-scripts": "4.0.3",
        "styled-components": "^5.2.3",
        "typescript": "^4.1.2",
        "web-vitals": "^1.0.1"
    },
    "devDependencies": {
        "@typescript-eslint/eslint-plugin": "^4.22.0",
        "@typescript-eslint/parser": "^4.22.0",
        "eslint": "^7.24.0",
        "eslint-config-airbnb": "^18.2.1",
        "eslint-config-prettier": "^8.2.0",
        "eslint-plugin-import": "^2.22.1",
        "eslint-plugin-jest": "^24.3.5",
        "eslint-plugin-jsx-a11y": "^6.4.1",
        "eslint-plugin-prettier": "^3.3.1",
        "eslint-plugin-react": "^7.23.2",
        "eslint-plugin-react-hooks": "^4.2.0",
        "eslint-plugin-testing-library": "^4.0.1",
        "husky": "^6.0.0",
        "lint-staged": "^10.5.4",
        "prettier": "^2.2.1"
    },
    "browserslist": {
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ],
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ]
    }
}

Problem


Solution

  • Well, what I had to do was this ..

    1. Install the cross-env library in development mode. This library causes the terminal to clear the buffer at the end of the test run in order to continue with the pre-push Hook.

    2. I inserted the following in the "test" scripts:

      "test": "cross-env CI = true react-scripts test --passWithNoTests"

    About CI = true

    This environment variable, short for Continuous Integration, is commonly defined in various CI environments, such as Travis CI and Github Actions, among many others.

    NOTE

    Do not use the --watchAll flag, as it will "lock" the terminal.