Skip to content

Instantly share code, notes, and snippets.

@travishorn
Last active May 4, 2022 21:59
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travishorn/5869188 to your computer and use it in GitHub Desktop.
Save travishorn/5869188 to your computer and use it in GitHub Desktop.
Using Grunt with Travis-CI

If you are using Grunt to run your tests and Travis-CI for your continuous integration, you will need to command Travis-CI to install the grunt-cli before running your test script(s). grunt-cli is meant to be a global package, so adding it to your dependencies or devDependencies is not ideal.

You'll need the following your package.json...

  • Test script command
  • Grunt and any plugins as dependencies

Like so:

{
  "name": "projectname",
  "version": "0.0.1",
  "description": "Project description goes here.",
  "main": "index.js",
  "scripts": {
    "test": "grunt test"
  },
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-jshint": "~0.6.0",
    "grunt-contrib-nodeunit": "~0.2.0"
  }
}

As an example, the following might be part of your test task in gruntfile.js:

module.exports = function(grunt) {
  grunt.initConfig({
    nodeunit: {
      all: ['test/**/*.js']
    },
    jshint: {
      all: ['src/**/*.js']
    }
  });

  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-nodeunit');

  grunt.registerTask('test', ['jshint', 'nodeunit']);
};

Finally, in order to run Grunt on Travis-CI, make sure your .travis.yml file looks similar to this:

language: node_js
node_js:
  - "0.10"
before_script:
  - npm install grunt-cli -g

Note the last line, this will cause Travis-CI to install grunt-cli as a global package before running the grunt test task. You will need to keep that line present in your .travis.yml file, since Travis-CI preserves no state between builds. This method was previously used by the Grunt project itself, and installing Grunt globally is still officially recommended.

@MaxdAyala
Copy link

When you say "This method is used by the Grunt project itself", this seems to have changed recently.

In the commit dated 7 Feb 2016 the Grunt .travis.yml file no longer uses this method of installing grunt-cli in the before_script property.

@travishorn
Copy link
Author

travishorn commented Jul 13, 2016

@MaxdAyala you are correct, they did change this. Their new method involves adding grunt-cli as a local dependency. What's interesting is that their official documentation still recommends installing it globally. I think I will leave this gist as it is, but add a note about the Grunt project no longer using this method, while still supporting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment