Hemanth.HM

A Computer Polyglot, CLI + WEB ♥'r.

Grunt Test Before Git Comit

| Comments

Avoid rewriting history with rebase or roll back and re-commit and use the below in your git/hooks/pre-commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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

In the Gruntfile.js (As of now I'm using mocha, so..)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module.exports = function(grunt) {

  // Add the grunt-mocha-test tasks.
  grunt.loadNpmTasks('grunt-mocha-test');

  grunt.initConfig({
    // Configure a mochaTest task
    mochaTest: {
      test: {
        options: {
          reporter: 'spec'
        },
        src: ['test/**/*.js']
      }
    }
  });

  grunt.registerTask('test', 'mochaTest');

};

Well ofcourse, this can be imporved for release tasks as well :-)

Comments