Automatically running tests before commits with ghooks

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

Wouldn't it be nice if everyone ran the tests before committing code? With ghooks, you can automatically add a githook when dependencies are installed which will allow you to define common scripts to be run at various points during git actions (like committing). In this lesson, we'll add a githook for running the tests before we commit code to make sure we don't commit anything that breaks the tests.

[00:00] Now that we have everything set up with TravisCI, we are ensuring that we're not releasing anything that breaks the test. One thing that would be really nice is to not actually forget to the run the test before we send it off to TravisCI to have it run. Make sure we get a tight feedback loop.

[00:18] One really awesome feature about git, is called githooks. You have different places where you can hook into git and execute some script before or after a certain action. What we'd like to do is run our test before we can actually commit any code. Make sure that we're not committing anything that could break.

[00:38] We're going to do this by using something called ghooks. One thing about githooks is that they're not actually committed to source. If we set up a githook locally and then somebody else clones our project, they won't have the same githooks that we have. Ghooks actually makes it so when you NPM install, it will install the githooks and you can define your githooks in your package JSON.

[01:04] Let's go ahead and install ghooks. NPM ID, ghooks. Now, we can look in our package JSON. We'll see ghooks is installed there. to configure ghooks, we add this config object with ghooks. We'll say, "Pre-commit," and we'll simply run our test single script. We'll NPM run, test single.

[01:28] Let's go ahead and break our test and see ghooks protecting us there. If I change this to a one for example, and then I run git commit all the things, it's going to run our test. Because our tests fail, we can't actually commit. Let's go ahead and fix our test.

[01:49] This NPM debug came from the fact that our NPM script failed. Let's go ahead and remove that. If we look at our git status, make sure that we're only modifying the package JSON. We'll look at the div. We have the ghooks config, ghooks. Let's go ahead and add everything. Look at our git status again.

[02:11] Let's run, "NPM run commit." This is a chore that's related to ghooks. Add ghooks and our description. We don't have any issues. Ghooks will run the test for us. If they pass, then our commit succeeds. We can push this up to a remote repository.

[02:31] If we look at our build, we see ghooks. That ghooks protect ourselves as I said. It will run our build. Everything should pass just fine. Now, we're protected because of ghooks.

[02:45] In review, all that you need to do is NPM install -d ghooks. That will add ghooks to your dev dependencies. Then, you configure ghooks via the config ghooks and then any of the githooks that you want to have run.

Mauricio Robayo
Mauricio Robayo
~ 6 years ago

Are you still using ghooks? (

Markdown supported.
Become a member to join the discussionEnroll Today