---
title: "Grunt Test Before Git Commit"
description: "Running tests with grunt before every git commit using pre-commit hooks. Practical notes and code examples on Grunt Test."
url: "https://h3manth.com/scribe/grunt-test-before-git-commit/"
canonical_url: "https://h3manth.com/scribe/grunt-test-before-git-commit/"
markdown_url: "https://h3manth.com/scribe/blog/grunt-test-before-git-commit.md"
publishDate: "2013-07-15T00:00:00.000Z"
author: "Hemanth HM"
category: "node"
tags: ["node","grunt","git","testing"]
word_count: 100
reading_time_minutes: 1
x_markdown_tokens: 271
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Grunt Test Before Git Commit

> Running tests with grunt before every git commit using pre-commit hooks. Practical notes and code examples on Grunt Test.

Avoid rewriting history with rebase or roll back. Use this in `.git/hooks/pre-commit`:

```bash
#!/bin/bash
curBranch=$(git rev-parse --abbrev-ref HEAD)

# Ignore rebase, stash unstaged.
[[ $curBranch != '(no branch)' ]] && git stash -q --keep-index && grunt test

# Check the exit status of grunt.
[[ $? -ne 0 ]] && echo "Test failed, can't commit" && exit 1

# All is well, stage and restore stash.
git add .
git stash pop -q

## Gruntfile.js
javascript
module.exports = function(grunt) {
  grunt.loadNpmTasks('grunt-mocha-test');
  
  grunt.initConfig({
    mochaTest: {
      test: {
        options: { reporter: 'spec' },
        src: ['test/**/*.js']
      }
    }
  });
  
  grunt.registerTask('test', 'mochaTest');
};
```
