Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include NodeJS tests into Gradle and Hudson?

We are a Scala/Java shop and we use Gradle for our build and Hudson for CI. We recently wrote some node.js code with tests in mocha. Is there anyway to get that included in our gradle workflow and setup in Hudson? I looked at the gradle-javascript-plugin but I could not figure out how to run npm test or npm install through it and not sure how to make it run through gradle-build or gradle-test commands and also let Hudson pick it up.

like image 548
pathikrit Avatar asked Sep 28 '12 17:09

pathikrit


People also ask

Should I include tests in NPM package?

Don't include your tests. Oftentimes tests are like 5x the size of the actual codebase. As long as your tests are on Github, etc, that's good enough. But what you absolutely should do is test your NPM package in its published format.

Can I use Jenkins for Nodejs?

js with Jenkins pipeline, we'll need the latest version of Node. js installed. Since Jenkins is a Java application, we'll also need Java installed. My sample application does not use Couchbase, this will ensure anyone can easily use this guide.

Is it npm test or npm run test?

TL;DR there is no difference. It's just a shortcut for npm tests which run the test command in the package. json file. npm run test performs the same action in this case.


1 Answers

I can get you part of the way there, I am mid-stream on this task as well. Make sure you have at least Gradle 1.2.

import org.gradle.plugins.javascript.coffeescript.CoffeeScriptCompile


apply plugin: 'coffeescript-base'

repositories {
  mavenCentral()
  maven {
    url 'http://repo.gradle.org/gradle/javascript-public'
  }
}

task compileCoffee(type: CoffeeScriptCompile){
  source fileTree('src')
  destinationDir file('lib')
}

Reference: http://gradle.1045684.n5.nabble.com/State-of-javascript-stuff-in-master-td5709818.html

Provided with a way to compile my coffeescript I can now add the npm install cmd into a groovy exec request and barf depending on the exec cmd result providing stdout/stderr

npm install
echo $?
0
npm install
npm ERR! install Couldn't read dependencies
npm ERR! Failed to parse json
npm ERR! Unexpected token }
npm ERR! File: /<>/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR! 
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse

npm ERR! System Darwin 11.4.2
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! cwd /<>/
npm ERR! node -v v0.8.14
npm ERR! npm -v 1.1.65
npm ERR! file /<>/package.json
npm ERR! code EJSONPARSE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /<>/npm-debug.log
npm ERR! not ok code 0
echo $?
1

Results in:

task npmDependencies {
  def proc = 'npm install'.execute()
  proc.in.eachLine { line -> println line}
  proc.err.eachLine { line -> println 'ERROR: '+line }
  proc.waitFor()
  if (proc.exitValue()!=0){
    throw new RuntimeException('NPM dependency installation failed!')
  }
}

As far as the mocha tests, I don't have first-hand knowledge of this, however I suspect you can handle similarly.

like image 98
techie.brandon Avatar answered Sep 17 '22 15:09

techie.brandon