Hemanth's Scribes

node

Grunt Test Before Git Commit

Author Photo

Hemanth HM

Thumbnail

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

#!/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');
};
#node#grunt#git#testing
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.