I added a script, which installs NodeJS and Karma if not already installed. This was done by adding a Pre build step of type 'Execute Shell'. Notice that I am also installing phantomjs to execute the tests using a headless browser.
# install nodejs, if using cloudbees (and if not already installed)
curl -s -o use-node https://repository-cloudbees.forge.cloudbees.com/distributions/ci-addons/node/use-node
NODE_VERSION=0.10.13 source ./use-node
ARCH=`uname -m`
node_name=node-${NODE_VERSION}-${ARCH}
# install phantomjs, karma
[ -d /scratch/jenkins/addons/node/$node_name/lib/node_modules/phantomjs ] || npm install -g phantomjs
[ -d /scratch/jenkins/addons/node/$node_name/lib/node_modules/karma ] || npm install -g karma
[ -d /scratch/jenkins/addons/node/$node_name/lib/node_modules/karma-junit-reporter ] || npm install -g karma-junit-reporter
[ -d /scratch/jenkins/addons/node/$node_name/lib/node_modules/karma-phantomjs-launcher ] || npm install -g karma-phantomjs-launcher
Note: I also had to add karma-junit-reporter and the karma-phantomjs-launcher plugins to the Karma configuration (karma.conf.js). My plugin configuration looked like this:
plugins : [ 'karma-jasmine', 'karma-chrome-launcher', 'karma-firefox-launcher', 'karma-junit-reporter', 'karma-phantomjs-launcher' ]
[ -d $HOME/bin ] || mkdir $HOME/bin
[ -f $HOME/bin/karma ] || ln -s /scratch/jenkins/addons/node/$node_name/bin/karma $HOME/bin/karma
[ -f $HOME/bin/node ] || ln -s /scratch/jenkins/addons/node/$node_name/bin/node $HOME/bin/node
Next was to execute the maven-karma-plugin. As I had a Maven build job, there was no possibility to configure a file pattern matching the Karma unit test reports. Jenkins would initially therefore not collect the reports and include them in the unit test output. I solved this by first configuring Karma (karma.conf.js) to place the reports in the target/surefire-reports folder:
junitReporter : {
outputFile : 'target/surefire-reports/karmaUnit.xml'
},
<plugin>
<groupId>com.kelveden</groupId>
<artifactId>maven-karma-plugin</artifactId>
<version>1.3</version>
<configuration>
<configFile>${basedir}/src/test/resources/config/karma.conf.js</configFile>
<browsers>PhantomJS</browsers>
</configuration>
<executions>
<execution>
<id>karma</id>
<goals>
<goal>start</goal>
</goals>
<!-- execute karma before test phase to let Jenkins collect the target/surefire-reports/karmaUnit.xml -->
<phase>process-test-classes</phase>
</execution>
</executions>
</plugin>
I've found inspiration in these:
I set this up, and was seeing errors when karma tried to run. I ended up discovering that the karma symlink path was wrong.
ReplyDelete[ -f $HOME/bin/karma ] || ln -s /scratch/jenkins/addons/node/$node_name/lib/node_modules/karma/bin/karma $HOME/bin/karma
Thanks for the post. Works well!