Thursday, September 24, 2009

Better server-side JavaScript unit testing

As part of a project aiming at a server and client-side tag library based Rhino, I thought it might be nice to write my unit tests in JavaScript. A quick Google search later and I find the non-uniquely named JsUnit project, which not only implements *unit in JavaScript, but has Ant and Maven 2 support. However, quite soon, I hit some problems, mainly around very little error reporting and feedback. Few things are as annoying as knowing there is a problem, but having no idea where.

A quick fork later and I now have JsUnit 1.3-db. So far, this fork adds:
* Callstack reporting for errors and failures
* Text test result output, not just XML
* File and line numbers in all error messages
* Upgraded Rhino

Previously, if you hit an error, you'd get this:
[INFO] [jsunit2:jsunit-test {execution: test}]
[INFO] ------------------------------------------------------------------------
Then, you'd have to dig into the target/surefire-reports/TEST-MyTestCase.xml to find this:
<testcase name="TestFieldTest.testRender" time="0.006">
<error message="ReferenceError: "bar" is not defined." type=""/>
Not helpful. Now, you get this:
[INFO] [jsunit2:jsunit-test {execution: test}]
TestRunner (1 test cases available)
> Starting test suite "TextFieldTest"
- Running test 1: "TextFieldTest.testRender"
ERROR in TextFieldTest.testRender: ReferenceError: "bar" is not defined. (TextField.js:7)

Stack trace:
1: TextField.js:7
2: TextFieldTest.js:6 (TextFieldTest_testRender)
3: JsUnit.js:827 (TestCase_runTest)
4: JsUnit.js:808 (TestCase_runBare)
5: JsUnit.js:386
6: JsUnit.js:409 (TestResult_runProtected)
7: JsUnit.js:390 (TestResult_run)
8: JsUnit.js:797 (TestCase_run)
9: JsUnit.js:1033 (TestSuite_runTest)
10: JsUnit.js:1014 (TestSuite_run)
11: JsUnit.js:2614 (EmbeddedTextTestRunner_run)
12: Components:4
< Completed test suite "TextFieldTest"
1 error, 0 failures.
[INFO] ------------------------------------------------------------------------
For fellow Maven folks, you can find the releases at my repo:


  1. Did you see the test runner that Shaoting cooked up for JavaScript tests?

    With this you can run them directly in your IDE using its built in JUnit support.

  2. Nice, we should fold that back into jsunit. It would be nice to get the surefire integration it provides without all the setup that is needed currently with his solution. Since his uses jsunit, I'm guessing it shares the same issues as I found wrt poor error reporting and the lack of call stack reports.

  3. you have a nice site.thanks for sharing this site. various kinds of ebooks are available here

  4. Wonderful article,thanks for putting this together! "This is obviously one great post. Thanks for the valuable information and insights you have so provided here. Keep it up!"
    How to seduce a woman

  5. The best recommendation you could make about the Oakley Crosshair sunglasses is the fact that some pilots insist on wearing them during flights, and obviously having unimpaired vision is fake oakley sunglasses important to them. This means that if this brand of sunglasses is considered good enough for a pilot, then they are likely to be able to do the job for just about anyone. Recommendations from those that wear the product are very important, so when you are thinking about getting a new set of sunglasses, bear this in mind.There are many reasons why consumers choose to buy branded products and aside from looking cool, knowing that the products are usually built to last is one very good reason.

  6. very useful codes, java script makes website more beautiful and attractive.. please share more codes like this