JavaScript Unit Testing with Hudson and JasmineBDD

Ingredients:

  • JasmineBDD, a JavaScript BDD/ TDD framework
  • The JUnitXmlReporter from Jasmine Reporters
  • Envjs (embedded with the Jasmine Reporters download)
  • Hudson Jenkins (continuous integration server) on a Linux slave

Recipe:

  • let Hudson execute a (Java) shell command
  • let Java run Envjs
  • let Envjs run a simple JavaScript which loads an HTML file (your JasmineBDD test runner)

Voila!

The Java command:
java -cp lib/envjs/js.jar:lib/envjs/jline.jar org.mozilla.javascript.tools.shell.Main -opt -1 -f lib/envjs/envjs.bootstrap.js -f test.js
(The libs and the bootstrap file came with Envjs in the Jasmine Reporters download.)

The test.js content:
window.location = 'SpecRunner.html';
(I.e., opening the JasmineBDD spec runner.)

Simple Unit Testing Only

So far, this only works for simple unit testing for me.  F.ex., it won’t play nice with prototype (which is really integration testing, but still).

It’s probably got something to do with Envjs’ limitations compared to a full web browser.

But hey, headless automatic JavaScript unit testing ain’t bad 🙂

Update:
@ingesol got Hudson to run Jasmine using JsTestDriver and Jasmine JsTestDriver Adapter.  (See guide to Hudson and JSTD.)

It’s not headless – requires running JSTD server and browsers, but it will handle prototype et al.

Update 2:

Headless testing as described above is not only for simple unit testing – jQuery works fine, too.  There’s advantages to running a JsTestDriver server, especially catching browser quirks, but for much of the BDD/ TDD effect (testable code, design, executable requirements, etc) you can run headless testing continuously in Hudson without any “external help”…
 

2 thoughts on “JavaScript Unit Testing with Hudson and JasmineBDD”

  1. Getting exceptions:

    java -cp ext/js.jar:ext/jline.jar org.mozilla.javascript.tools.shell.Main -opt -1 -f test/envjs.bootstrap.js -f test.js
    js: “test/envjs.bootstrap.js”, line 1: Couldn’t read source file “../ext/env.rhino.1.2.js: ../ext/env.rhino.1.2.js (No such file or directory)”.
    js: uncaught JavaScript runtime exception: ReferenceError: “Envjs” is not defined.

    js: “test.js”, line 1: uncaught JavaScript runtime exception: ReferenceError: “window” is not defined.
    at test.js:1

    kahmed@vm-jenkins-staging ~/jasmine-reporters $ cd ext
    kahmed@vm-jenkins-staging ~/jasmine-reporters/ext $ ls
    env.rhino.1.2.js jasmine.css jasmine-html.js jasmine.js jline.jar js.jar

    then, i installed envjs, npm install envjs, but now i get the exception:

    java -cp ext/js.jar:ext/jline.jar org.mozilla.javascript.tools.shell.Main -opt -1 -f test/envjs.bootstrap.js -f test.js
    Exception in thread “main” java.lang.NoClassDefFoundError: org/mozilla/javascript/tools/shell/Main
    Caused by: java.lang.ClassNotFoundException: org.mozilla.javascript.tools.shell.Main
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Could not find the main class: org.mozilla.javascript.tools.shell.Main. Program will exit.

Leave a Reply

Your email address will not be published. Required fields are marked *