javascriptunit-testingrhinoqunit

In javascript, how can you assert that two items are equal?


I am running Rhino and trying to determine how to do an assert_equals() or its equivalent in Javascript. From the Rhino shell I can load qunit, but I can't create an assertion that will fail.

What is the easiest way to use assertions in Javascript? I will be using Javascript in environments other than a web browser, so knowing how to do this from the Rhino shell would be very helpful.

Example of what does not work.

js> load("qunit.js")
js> equals(5,4,"message")
js> 
js> 

Solution

  • It's fairly easy to implement your own assert() function in javascript which throws an exception when the assertion fails. The implementation documented here is fairly concise and should do the job.