The most common reason for failed tests on CI is when a developer forgets to run the tests locally. Code in a repository can contain bugs, that's a given, but at least it should never have bugs that could be found by tests, so running tests before a commit is a mandatory practice in lots of companies.
Let's follow this best practice. We will use a library called Husky, which can wire to Git hooks to run desired scripts.
We start with packages, as always:
$ npm install husky --save-dev
Next, we need to set scripts in package.json:
// package.json
{
"scripts": {
"test": "NODE_ENV=test jest",
"precommit": "npm test"
}
}
And this is it; from now on, before any commit, Husky will run your tests. So, there will be fewer surprises after pushes.