BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Article: JavaScript Test Driven Development with JsUnit and JSMock

| by Dio Synodinos Follow 2 Followers on Feb 09, 2009. Estimated reading time: 1 minute |

This article by Dennis Byrne is a crash course in writing maintainable JavaScript. Dennis adds features to a running example by iteratively following a simple principle: write a unit test, make it pass. Each test serves as a quality feedback loop, creating both a safety net and an executable form of documentation for anyone who wants to change the production code. By starting each feature with a simple failing test he ensures that all features are tested. He avoids the cost of rewriting code to test it later. This is particularly valuable given the fact that JavaScript developers have so much rope to hang themselves with - consider how much global mutable state there is between the DOM API and the language itself.

Our running example is a casino slot machine with three reels.  Each reel has five possible states, represented by images.  Each reel randomly assumes a state when the play button of the slot machine is pressed. The slot machine balance will be incremented or decremented depending on whether or not all three reel states are equal.

Our tools will be stubs, mock objects and a little bit of dependency injection. We will use JsUnit to run unit tests and a JavaScript mock object library called JsMock. Integration testing, a compliment of unit testing, is beyond the scope of this article. This does not mean integration testing is less important - just that we will strive for quick feedback rather than the slower and more comprehensive feedback obtained with tools like Selenium and Watir.

Read the full article here: JavaScript Test Driven Development with JsUnit and JSMock

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT