Blog

Jenkins with NodeJS and Grunt

By:
on:

Continuous integration, especially with Jenkins, is fun ... once you have everything set up.

To start a new project, you configure your local development environment. Once Grunt nicely reruns your Mocha tests, it's time to start thinking about the CI configuration.

You need to automate the installation steps of your Javascript project. The easiest way is to execute a bash-script. This prevents missing or outdated packages.

This is my script:

#!/bin/bash



export PATH=/usr/local/bin:/path/to/node:/path/to/node_bin:/path/to/phantomjs:/path/to/jscoverage:$PATH;



mkdir -p build

npm install

grunt jenkins

It executes these steps:

  1. Create the 'build'- directory if it doesn't exist
  2. Install all required Node packages
  3. Run the Grunt jenkins task

Why a seperate Jenkins task in Grunt?

On your local development environment, you expect clear, human readable output. Jenkins on the other hand works very well with XML. This is why we need a separate task.

This is an example Gruntfile:

'use strict';



module.exports = function(grunt) {



  // Project configuration.

  grunt.initConfig({

    mochacov: {

      default: {

        src: ['test/**/*.js'],

        options: {

          reporter: 'spec'

        }

      },

      jenkins: {

        src: ['test/**/*.js'],

        options: {

          reporter: 'xunit',

          output: 'build/test-result.xml'

        }

      },

      cobertura: {

        options: {

          reporter: 'mocha-cobertura-reporter',

          require: ['chai'],

          output: 'build/cobertura.xml',

          coverage: true

        },

        src: ['test/**/*.js']

      },

      coveragehtml: {

        options: {

          reporter: 'html-cov',

          require: ['chai'],

          output: 'build/coverage.html'



        },

        src: ['test/**/*.js']

      }

    },

    jshint: {

      options: {

        jshintrc: '.jshintrc'

      },

      default: ['Gruntfile.js', 'lib/**/*.js'],

      jenkins: {

        options: {

          reporter: 'checkstyle',

          reporterOutput: 'build/checkstyle-result.xml'

        },

        src: ['Gruntfile.js', 'lib/**/*.js']

      }

    }

  });



  grunt.registerTask('jenkins', ['jshint:jenkins', 'mochacov:jenkins', 'mochacov:coveragehtml', 'mochacov:cobertura']);



};

In your jenkins task, you can now configure the shellscript to run:

sh jenkins-runner.sh

Now, setup a few Post-build actions as shown in the image.

Jenkins Post-Build steps

As you see, we use Cobertura to generate coverage reports. Any other plugin that generates an XML coverage report is fine.

Do you have another way to run your Mocha tests on Jenkins?

KOEN IS HAPPY TO INTRODUCE APPSALOON
APPSALOON BV
Bampslaan 21 box 3.01
3500 Hasselt
Belgium
VAT: BE08 4029 5063
2021 © AppSaloon All Rights Reserved
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram