Enforcing a standard for commit messages with a commit-msg hook using something like commitlint is useful, but ideally if we have a commit message hook set up to lint the commit message, it should run first, before any test suite.
That is, if we have a large unit test suite which must pass before committing is possible, and it takes a long time to run (i.e. a minute or two), it is better to have a commit message problem fail first, quickly. It is annoying to run through a large, slow test suite successfully only to have a commit message lint prevent the entire commit at the end!
It is not possible to change the order of git hooks execution. Git does not have access to the commit message at that stage of processing.
However, using commitlint and Husky, we can achieve the desired order by simply using the commit-msg hook to trigger our tests instead of the pre-commit hook.
Inside .huskyrc (or the “husky” field inside package.json) we can change this:
{ "hooks": { "commit-msg": "commitlint -e $GIT_PARAMS", "pre-commit": "npm test" } }
Into this:
{ "hooks": { "commit-msg": "commitlint -e $GIT_PARAMS && npm test" } }
The commit-msg hook will run before the commit is actually made, so this is still a pre-commit hook requiring successful run of the unit tests before one can commit.
But any problem with the commit message itself will show up immediately.