Ingredients:
- JasmineBDD, a JavaScript BDD/ TDD framework
- The JUnitXmlReporter from Jasmine Reporters
- Envjs (embedded with the Jasmine Reporters download)
HudsonJenkins (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”…
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.
Hi Kamal,
this is an old post, and i don’t have any specific help for you.
perhaps you could take a look at http://build-doctor.com/2010/12/08/javascript-bdd-jasmine/ – @builddoctor took it a bit further, and there seems to be some advice on alternative ways of doing it in the comments as well.