HTML5 Zone is brought to you in partnership with:

I am a web developer at Crytek GmbH. As for my open source contributions, some were pretty popular once Sapid CMS, AOP library for PHP, few plugins for jQuery. Numerous of my works were published in specialized medias, I have been a speaker at developer conferences (e.g. PHP Conf), and published articles in magazines such as PHP Architect and PHP Magazine International Dmitry is a DZone MVB and is not an employee of DZone and has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

Review on JavaScript Unit Testing

02.26.2013
| 2066 views |
  • submit to reddit

As client-side application is getting more and more complex, front-end automated testing is more in demand. Before deployment we have to validate HTML according to given DTD, lint CSS and JavaScript, run performance tests, run unit-tests and do functional testing. Front-end unit testing has its quirks. We have to run tests on every browser and platform defined by our requirements. That is supposed to be automated and it's no easy task. There two widely spread approaches applicable in CI: Selenium Remote Control, which fires up any number of browsers, navigates to specific pages and performs tests and TestSwarm, where any number of clients listen for testing tasks on the server, perform test when requested and return results back to the server. As a budget solution, one can use Rhino with env.js, which emulates a the common browser environment.

Besides, JavaScript units don't often fit the model "assign function arguments and check the result code". Let's take a generic widget. It is being fired on some event (e.g. DOM is ready), gets references on required nodes by provided selectors, modifies DOM if necessary (render UI), subscribes handlers for specific events (button clicked, control value changed, key pressed). Thus to test a widget, we have to make a fixture emulating some initial state of DOM, check if the widget managed to find all the required nodes, check if it modified DOM as intended and trigger events to check handlers, provide mock end-points for Ajax-requests.

I recently laid the hands on, JavaScript Unit Testing book by Hazem Saleh. It gives excellent introduction into such testing frameworks as Jasmine, YUI Test Suit, QUnit, JsTestDriver. I would definitely add to this list Mocha , but whatever. Every framework is nicely covered and provided with test examples showing different cases: simple methods, asynchrononous methods, widget. What I'm unhappy with is the first chapter. I would begin with JavaScript unit-testing is a part of front-end automated testing, which is a part of Continuous Integration. The only introduction to CI the authors gives is a few-lines long obscure tip. Actually he provides examples of YUI Test / JsTestDriver integration with build tools, but it feels out of place to me, without a decent cover of CI. By the way, as far as I see, build-script examples are oriented on Windows. That's weird. I wouldn’t expect the majority of fronteers build on Windows.

The second thing I don't like about the chapter is that the author just after unit-testing/TDD introduction jumps to the example details and, by the way, the first framework following utilizes BDD. Thus, it would be just proper to explain first that the testing frameworks described below in the book based on *Unit and BDD styles (and QUnit is rather a custom style) and give some input regarding the styles.

What I found personally useful is the chapter about JsTestDriver. Of course I had heard about that one, but never bothered to go into details. The chapter puts it clear how one can exploit the solution. And it seems as a good alternative for cross-browser tests to Selenium RC (Selenium is still designed for functional testing) and Browserstack, which costs pretty money.



Published at DZone with permission of Dmitry Sheiko, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)