Running Dojo 1.7+ DOH unit tests on the command line with Rhino

October 09, 2012 [Dojo, JavaScript, Tech, Test Driven]

To run your own DOH-based unit tests on the command line using Rhino:

NOTE: this is Dojo 1.7 and above. For 1.6, there was a whole other cryptic incantation.

Project layout

Imagine your code is somewhere different from dojo, and another library you use is somewhere else:

C:/code/mycode/org/me/mytests/
                             ...
                             mytestmodule.js
                             ...
C:/code/mycode/org/them/nicelib/
                             ...
C:/libs/dojo/dojo/
                 ...
                 dojo.js
                 ...
             dijit/
                 ...
             dojox/
                 ...
             util/doh/
                     ...
                     main.js
                     ...

Config file

Yes, you need a config file. Imagine it's at C:/code/mycode/dohconfig.js and it looks like this:

require({
    paths: {
        "org/me" : "../../../code/mycode/org/me",
        "org/them" : "../../../code/mycode/org/them/nicelib"
    }
});

Command line

Now you can run your tests like this:

java -jar C:/libs/dojo/util/shrinksafe/js.jar C:/libs/dojo/dojo/dojo.js baseUrl=file:///C:/libs/dojo/dojo load=file:///C:/code/mycode/dohconfig.js load=doh test=org/me/mytests/mytestmodule

Explanation

Comments

rajesh 2013-04-16

Hi,
Is it possible to load the dojo into the java application with the help of Rhino adn envjs?
i’m in a need to render the dojo charts on server side. when trying to load the dojo into java via rhino, an Exception rises which is as follows,
"org.mozilla.javascript.EcmaError: TypeError: Cannot read property "parentNode" from undefined "
Im struggling to find a way to load Dojo. Please help me

Andy Balaam 2013-04-16

Hi rajesh, I’d be surprised if that worked. I suggest using a hidden web browser control?

m 2012-10-16

Excellent article…is there a way to read the unit test results from the command line and log them to a text/xml file? Thanks

Andy Balaam 2012-10-17

Thanks! To log the output into a text file, just add " > log.txt" at the end of the command (or is that not what you meant?)

Ricardo Lucio 2013-01-21

Excellent article!!

One question: when I try to run a test that touch the dom (e.g.: when I create a dojox.mobile.Button in my test) it complains that window is undefined.
After some tests I noticed that the rhino engine didn’t provide a dom implementation.
Am I missing someting?

Andy Balaam 2013-01-22

Hi Ricardo, thanks! You are not missing something. There is no DOM when running in Rhino. env.js (http://www.envjs.com/) might do what you need, if you want to do tests of DOM-manipulating code on the command line. There’s still a lot you can’t do without a real-live browser, though.