score:-1

to do locally you can just write a script which first runs the tests and pipes the exit code and decides whether to really push or not based on the exit code.

by default npm test runs in interactive mode

to exit after running the tests use ci=true

i.e. ci=true npm test

score:-1

this worked for me:
npm install --save-dev pre-commit
https://www.npmjs.com/package/pre-commit

then, in package.json:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "ci=true react-scripts test", //<-- update
    "eject": "react-scripts eject",
  }

adding ci=true runs all tests without making it interactive

finally,

"devdependencies": {
    "pre-commit": "test"
  }

score:0

maybe you can utilise some npm packages for this.

so, this is not a direct solution but you can include as many things you want instead of using git commands, shells and yml files

install package pre-commit and install git-cz from npm. using these you can make use of pre-commit and commit in package.json and desired things to them.

now you can make use of these packages in your packages.json like below

{ "start": "node index.js", "pre-commit": "lint-staged", "commit": "git-cz", "lint": "eslint . --ext .js,.jsx", }

for example you want to run test cases then pre-commit: npm run test && lint-staged

because in our project we needed to update documents, checking style-lint, eslint and test cases, so we were using these combination.

but you should not commit directly using git commit -m "message" but with npm run commit.

hope, this helps.

score:9

you need to use husky package. here is basic configuration (put it in package.json). it adds pre-commit hook to your git configuration.

"husky": {
  "hooks": {
    "pre-commit": "ci=true npm run test",
  }
}

you can also consider using lint-staged to lint files which you commit. you can see full configuration here.


Related Query

More Query from same tag