pathikrit pathikrit - 2 months ago 13
Node.js Question

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.

Answer

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.