Jenkins ♡ node.js

With my colleague (from InGo) we are building new small startup which will help (we hope) to people to solve «problem of 1000 tabs opened» (usually to read them later).

Our server are built with node.js express application and for CI we are using Jenkins. This post is about how we make Jenkins understand node.js test tools results.

First I have installed some plugins:

  • AnsiColor — this one to have a nicer console output when it is available
  • Jenkins Cobertura Plugin — code coverage
  • Jenkins Violations plugin — for jshint/jslint warnings
  • xUnit plugin — test’s results

To do not spend time each time to edit jenkins job I created Makefile and added tasks to it.

Test results with xUnit plugin

For tests we are using mocha and should.js (and supertest for http requests). This is a job to get xUnit compatible xml:

test-jenkins-xunit:
	@NODE_ENV=test ./node_modules/.bin/mocha \
		--recursive \
		--check-leaks \
		--reporter xunit \
		$(MOCHA_OPTS)

In build steps I add shell script step:

make test-jenkins-xunit 1> results/xunit.xml

And in post build steps:
Jenkins xUnit plugin usage

JsHint warnings with Violations plugin

First again Makefile task:

test-jenkins-jslint:
	./node_modules/.bin/jshint $(JS_FOLDERS) --jslint-reporter

And how to use it in jenkins build step:

make test-jenkins-xunit 1> results/jshint.xml || exit 0

I added ‘exit 0’ because it return 1 if anything found and build step will fail.

Exists one problem with Violations plugin and xml that produced by jshint. Because it uses relative path you cannot see in job report where actual problem is. I saw several posts about it and nobody publish solution. After previous build step i have added new one that fix xml:

sed -E "s?<file name=\"(.*)\?\">?<file name=\"$WORKSPACE/\1\">?" results/jshint.xml > results/jshint-fixed.xml

This will replace relative paths in name attribute to absolute that Violations Plugin can handle.

Now after build step:
Jenkins Violations Plugin usage

Code coverage with Cobertura plugin

Makefile task:

test-jenkins-cov:
	@NODE_ENV=test ./node_modules/.bin/istanbul cover --report cobertura --dir ./results ./node_modules/.bin/_mocha -- \
		--recursive \
		--check-leaks \
		--reporter $(REPORTER) \
		$(MOCHA_OPTS)

Build step is very simple:

make test-jenkins-cov

Last thing after build step:
Jenkins Cobertura Plugin Usage

Now I press ‘Save’ and run this job to see reports (I hope You don’t forget to add repository to this job). Click on job name:
Jenkins results
When I click on any of this charts i can see more detailed report. This one from Violations Plugin that you can see when click on file in report (and you will not see this if you do not fix problem with relational paths).
Example of Violations

That is all! I hope this post will help to somebody. I decide to write it because there is no similar post that describe whole process.

Оставьте комментарий