Follow Slashdot stories on Twitter

 



Forgot your password?
typodupeerror
Image

Book Review: Test-Driven JavaScript Development 55

Posted by samzenpus
from the read-all-about-it dept.
eldavojohn writes "Test-Driven JavaScript Development by Christian Johansen is a book that thoroughly guides the user through some of the more advanced aspects of the JavaScript language and into Test-Driven Development (TDD). Throughout it, Johansen introduces great methods and utilities like libraries to accomplish all aspects of TDD in JavaScript. The book begins with Johansen demonstrating and teaching the reader some of the more advanced aspects of JavaScript to ensure that the following lessons in TDD are well understood. The best part of the book is in the last half where Johansen builds a chat client and server completely out of JavaScript using TDD right before the readers' eyes." Keep reading for the rest of eldavojohn's review.
Test-Driven JavaScript Development
author Christian Johansen
pages 475
publisher Addison-Wesley Professional
rating 9/10
reviewer eldavojohn
ISBN 978-0-321-683915
summary An in depth look at Test Driven Development in JavaScript.
First off the audience for this book are JavaScript developers interested in TDD. More specifically, I would identify the audience being the poor developers that have slaved over JavaScript for endless hours only to find out that there are 'discrepancies' in how their JavaScript functions in one browser versus another (or even across versions of the same browser). If you've ever came into work one day to learn that the latest version of Internet Explorer or Mozilla Firefox now throws errors from the deep recesses of your code and you have absolutely no idea where to start, then this book may be an item of interest to you. After all, wouldn't it be great to pull up the new browser and simply watch all your tests complete code coverage with glaring red results listing specific problematic locations?

Secondly, I'd like to establish that I'm writing this review with two key assumptions. The first assumption is that JavaScript is not in and of itself evil. You might hate JavaScript (as did I at one time) but it's a very flexible and enjoyable language when you're not battling some crazy 'feature' that a particular JavaScript engine exhibits or some issue with the dreaded Document Object Model (DOM). The second assumption is that TDD is a net positive when done correctly. To some, it may be a hard sell and the author of the book is no blind preacher. TDD has its pitfalls and the book adequately notes these claiming that TDD can actually work against you if used improperly. Feel free to wage wars in the comments debating whether or not the average JavaScript monkey is capable of avoiding pitfalls and learning to write good unit tests — I'm not getting sidetracked in this review on those topics.

This book is divided into four parts. The first part of the book gives you a slight taste of testing right off the bat in chapter one (Automated Testing). Johansen starts by showing a strftime function written in JavaScript and demonstrates briefly the very clumsy standard method of testing the method in a browser. From there he introduces Assertions, Setup, Teardown and Integration Tests. What I particularly enjoyed about this book is that these key components are not forgotten after introducing them, Johansen constantly nods to the reader when duplicate code could be moved to Setup or Teardown.

Chapter two is devoted to 'turning development upside-down.' This chapter analyzes the mentality of writing a test, running the test, watching it fail, making the test pass and then refactoring to remove duplication (if necessary). Johansen stresses and restresses throughout the book that the simplest solution should be added to pass the test. Fight the urge to keep coding when you are sure what comes next and just make sure you have unit tests for that new code. The third chapter runs through many test frameworks in JavaScript and settles in on JsTestDriver weighing the pros and cons of each option. Lastly, it is demonstrated how to use JsTestDriver both inside Eclipse and from the command line (something I deeply appreciated). Chapter Four expands on this by proposing learning tests which are tests that you keep around to try out on new browsers to investigate what you depend on. I'm not entirely sold on this practice but this chapter is definitely worth the look at performance testing it provides in a few of the more complete aforementioned frameworks.

The next 145 pages are devoted to the JavaScript language itself. The reader will find out in later chapters why this was necessary but this second part felt too long and left me starving for TDD. There's a ton of great knowledge in these chapters and Johansen demonstrates an impressive display in his understanding of ECMAScript standards (all versions thereof) and all the JavaScript engines that implement them. In the following four chapters, the reader is shown the ins and outs of scope, functions, this, closures, anonymous functions, bindings, currying, namespaces, memorization, prototypical inheritance, tons of tricks with properties, mixins, strict mode and even the neat features of tddjs and JSON. What I was most impressed with in this chapter was how much care Johansen took with noting performance pitfalls in all of the above. Example: "closures in loops are generally a performance issue waiting to happen" and on for-in arrays he says "the problem illustrated above can be worked around, as we will see shortly, but not without trading off performance." Johansen seems tireless in enumerating the multitude of ways to accomplish something in JavaScript only to dissect each method critically. If you skip these sections, at least look at 6.1.3 as the bind() implementation developed there becomes critical throughout much of the book's code.

Chapter nine provides yet more dos and do nots in JavaScript with a tabbed panel example that demonstrates precisely what obtrusive JavaScript is and why it is labeled as such. Chapter ten is definitely not to be skipped over as it provides feature detection methods (specifically with regard to functions and properties) that are seen in later code snippets. Part two is devoid of any TDD yet rich in demonstrating the power of JavaScript. This is where the book loses a point for me as this seemed too long and a lot of these lessons — though informative — really seemed like they belonged in another book on the JavaScript language itself. I constantly wondered when I would start to see TDD but to a less experienced developer, these chapters are quite enlightening.

In the third part, we finally get to some TDD in which an Observer Pattern (pub/sub) is designed using tests with incremental improvements in true TDD fashion. Most importantly to the audience, we encounter our first browser inconsistencies that are tackled using TDD. This chapter illustrates how to make your first tdd.js project using the book's code and build your first tests followed up with the isolation of the code into setup and teardown functions. Rinse, wash, repeat for adding observers, checking for observers and notifying observers (all key functionality in the common observer paradigm). This is a great pragmatic example for TDD and the chapter wraps up with error checking and a new way to build a constructor. As we do this, we have to make changes to the tests and Johansen illustrates another critical part of TDD: fixing the tests after you've improved your code.

The twelfth chapter takes our Ajax friend the XMLHttpRequest object and gives it the same treatment as above. Of course, you might know it as the Msxm12.XMLHTTP.6.0 object or a variety of names so this is where our browser differences are exposed. On top of that, we're exposed to stubbing in order to test such an object. The author explores three different ways of stubbing it while building tests for GET requests. After building helpers to successfully stub this, we move on to POST, finally send data in a test and then pay attention to the testing of headers. Personally these two chapters were some of the best in the book and illustrated well a common method of utilizing TDD and stubbing to build up functional JavaScript.

Chapter thirteen builds on the previous chapter by examining polling data in JavaScript and how we might keep open a constant stream of data. Before jumping to the solution, the author investigates strategies like polling intervals and long polling which have their downfalls. We eventually come to the Comet client (which uses JSON objects) and build up our test cases that support our development of our new streaming data client. One important aspect brought up is the trick of using the Clock object to fake time. This was completely new to me and very interesting in simulating time with tick() to quickly fake and test expected lengths of time.

Chapter fourteen was definitely outside of my comfort zone. JavaScript on the server-side? Blasphemy! Johansen begins to bring together the prior elements to form a fully functional chat server all in JavaScript through TDD. In this chapter the reader is introduced to node.js and a custom version of Nodeunit the author modified to make a little more like JsTestDriver. The controller emerges through the TDD cycles. Responses to POST, adding messages, the domain model and even storage of data are given test cases to insure we are testing feature after tiny feature. Toward the end of the chapter, an interesting problem arises with our asynchronous interface. In testing it, how do we know what will result from a nested callback? Johansen introduces the concept of a Promise which is a placeholder that eventually provides a value. Instead of accepting a callback, the asynchronous method returns a promise object which is eventually fulfilled. We can now test adding messages in asynchronous manner to our chat room. The chapter builds on the chat server to passable functionality — all through TDD.

Chapter fifteen concentrates on building the chat client to the above server and in doing so provides the reader with TDD in regards to DOM manipulation and event handling. This chapter finally covers some of the more common problematic aspects of client-side JavaScript. Again, this chapter yielded many tricks that were new to me in TDD. JsTestDriver actually includes two ways to include HTML in a test and Johansen shows how to manipulate the user form on a page in order to test it automatically. The client is developed through TDD and node-paperboy is called in to serve up static files through http with Node.js. The message list displayed in the client is developed through TDD and then the same process used on the user form is done with the message form submission. The author brings in some basic CSS, Juicer and YUI Compressor to reduce all our work down into a 14kB js file containing an entire chat client. With gzip enabled it downloads at about 5kB. Potent stuff.

I was sad that more pages weren't spent on the final section. Chapter sixteen further expounds upon mocking, spies and stubbing. It lists different strategies and how to inject trouble into your code by creating stubs that blow up on purpose during testing. And we get a sort of abbreviated dose of Sinon, a mocking and stubbing library for JavaScript. The author repeats a few test cases from chapter eleven and moves on to mocking. Mocking is mentioned throughout the book but is passed over due to the amount of work required to manually mock something. The chapter ends with the author saying 'it depends' on whether you should use stubbing or mocks but it's pretty clear the author provides stubbing as he enumerates the pros and cons of each.

Chapter seventeen provides some pretty universal rules of thumb to employ when using TDD. From the obvious revealing intent by clear naming to strategies for isolating behavior, it's got good advice for succeeding with TDD. This advice aims to improve readability, generate true unit tests that stay at the unit level and avoid buggy tests. It's worth repeating that he gives a list of 'attacks' for finding deficiencies in tests: "Flip the value of the boolean expressions, remove return values, misspell or null variables and function arguments, introduce off-by-one errors in loops, mutate the value of internal variables." Introduce one deficiency and run the tests. Make sure they break when and where you would expect them to or your testing isn't as hardened as you might expect. Lastly the author recommends using JsLint (like lint for C).

There's a lot of information in this book but I think that the final examples were actually too interesting for my tastes. Often I grapple with the mundane and annoying parts of client side DOM — nothing on the server side. While this might change at some point in the future, I couldn't help but feel that the book would have been better with additional examples of more common problems than a chat client in JavaScript. I was certainly impressed with this example and it will hold the readers' attention much more than what I desire so I feel comfortable recommending this book with a 9/10 to anyone suffering from browser inconsistencies or looking to do TDD in JavaScript.

You can purchase Test-Driven JavaScript Development from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

*

This discussion has been archived. No new comments can be posted.

Book Review: Test-Driven JavaScript Development

Comments Filter:
  • Letting it all out (Score:5, Interesting)

    by Anrego (830717) * on Monday March 28, 2011 @01:28PM (#35642780)

    I know I’m out of the web dev loop, but as I recall, in most cases the web mantra seems to be to crank it out as quick as possible before it’s obsolete. The exposure I’ve had (which admittedly is a small sampling) leads me to believe that even basic core principles of traditional software development are thrown out the window in a mad dash to get something that “looks right”.

    Certainly the tools that have become the most popular around web dev would spell this out. Javascript, PHP, python, etc. Weakly types languages with loose structure, built in shortcuts, and lots of automagic. The whole concept of prototype based coding is to me just another example of rapid development trumping solid coding.

    And I’ve heard the “compensate with better QA” or the even more ludicrous “just hire people who never make mistakes” arguments. I’ll take my programming with strong typing and lots of built in idiot-proofing please, and I’m not afraid to admit that even with years of dev experience, I still make the occasional bone headed mistake and I figure the more chances it gets caught, the better.

    I think it’s good that _someone_ is trying to mix solid software development with web whimsy, but I don’t see it catching on, even in places where it really should!

    I’d like to note, that while this may start a flame war (or get harmlessly modded down to oblivion), this really wasn’t my intention.

    I’d also like to note, that I dislike TDD in general (yeah, I know, another flame war)! In my experience, most bugs I find are from running through some kind of manual procedure and noticing something ”odd” that an automated system wouldn’t have picked up. I imagine in web design, where the end result is mainly visual, this would be even more common. Sure, the table has the right values but they are all cut off and too tiny to read.

    I do like broad, interface level unit testing (if you can call it that) to test that inputs into the system as a whole result in appropriate output, as these kind of requirements rarely change but individually testing classes just adds extra work, extra bulk when making changes, and never seems to find anything! I have run into this so many times, where someone comes up with a really great way to refractor some code, only to find out that while the code change would be trivial, updating 50 unit tests wouldn’t so it doesn’t happen.

    And I can come up with much better and more productive ways to document behaviour and coordinate between developers (written documents, java style interface classes/etc) than a bunch of cumbersome unit tests.

  • by Anrego (830717) * on Monday March 28, 2011 @01:51PM (#35643110)

    I kind of lump TDD in with traditional older-style thinking.

    I admit I tend to have a kind of waterfall mentality. You spend months documenting _exactly_ what is needed, months designing the thing down to the tiny component parts, then proceed to basically paraphrase the design into code form. Slow, inflexible, inefficient ... and everything is moving away from it.

    TDD seems to be a relic of this approach. It would assume you have that first part, a very detailed list of everything you need, up front, before implementation. I've never understood how this is moving forward. To me we should be moving towards approaches that let us change requirements on the fly without it being a massive undertaking. Agile types may feel free to educate me as to why I'm an idiot here! To me TDD binds hands just as effectively as the miles of design and requirement docs did, and this seems like a bad thing.

    As for the bulk of your comment, I think we need a middle ground. Less hectic and "seat of your pants" ish, but not process overkill. The old "good, cheap, fast - pick 2" argument... but we need a little bit more "good".

  • A Few Responses (Score:5, Interesting)

    by eldavojohn (898314) * <eldavojohnNO@SPAMgmail.com> on Monday March 28, 2011 @02:13PM (#35643368) Journal

    I know I’m out of the web dev loop, but as I recall, in most cases the web mantra seems to be to crank it out as quick as possible before it’s obsolete.

    I think ECMAScript standards are here to stay for quite a while. I wouldn't worry about too much changing between 5 and 6. This book actually considers all versions of ECMAScript and ES5 was standardized in 2009 [wikipedia.org], 10 years after the last standardized version. While it takes a while for JavaScript engines to catch up on implementing those standards, I wouldn't spin this sort of stuff as "crank it out quick."

    The exposure I’ve had (which admittedly is a small sampling) leads me to believe that even basic core principles of traditional software development are thrown out the window in a mad dash to get something that “looks right”.

    Certainly the tools that have become the most popular around web dev would spell this out. Javascript, PHP, python, etc. Weakly types languages with loose structure, built in shortcuts, and lots of automagic. The whole concept of prototype based coding is to me just another example of rapid development trumping solid coding.

    I believe this is a false dichotomy that may have been true years ago. Once you see the maturity level of some JavaScript projects that companies like Google are working on, JavaScript doesn't have to be the slap dash crap you are speaking of. It just becomes clear that the language produces products equivalent to how much time is put into it. Here's one of many good counterexamples to your rule [google.com]. I am interested though: how do you define "solid coding?" That sounds like an ambiguous phrased designed to pick and choose language features as the user personally desires. The recent removal of OO programming from CMU's curriculum might point out how yesterday's mentality goes out of style and comes back in two decades later.

    And I’ve heard the “compensate with better QA” or the even more ludicrous “just hire people who never make mistakes” arguments. I’ll take my programming with strong typing and lots of built in idiot-proofing please, and I’m not afraid to admit that even with years of dev experience, I still make the occasional bone headed mistake and I figure the more chances it gets caught, the better.

    I think it’s good that _someone_ is trying to mix solid software development with web whimsy, but I don’t see it catching on, even in places where it really should!

    Ima let you finish. But I just want to say that that is a totally respectable position. Me, personally, I recognize that I have a huge diverse toolbox full of all sorts of tools. Some better for jobs inside the browser, some better for servers and some better for embedded systems. But I would ask you to consider that it's going to be a while before any of these tools to be your silver bullet.

    I’d like to note, that while this may start a flame war (or get harmlessly modded down to oblivion), this really wasn’t my intention.

    I’d also like to note, that I dislike TDD in general (yeah, I know, another flame war)! In my experience, most bugs I find are from running through some kind of manual procedure and noticing something ”odd” that an automated system wouldn’t have picked up. I imagine in web design, where the end result is mainly visual, this would be even more common. Sure, the table has the right values but they are all cut off and too tiny to read.

    There are certainly problems with rendering and layouts being difficult to automatically test. But there are a lot of tools out there (like firebug) that help you pin down what is going wrong in certain browsers. While the book doesn't delve deeply into it, it's something that is easier to deal with than, say, a weir

  • by slim (1652) <john@hart n u p.net> on Monday March 28, 2011 @03:02PM (#35643942) Homepage

    TDD seems to be a relic of this approach. It would assume you have that first part, a very detailed list of everything you need, up front, before implementation.

    You seem to have a misapprehension of TDD. TDD is very agile; you write tests as a way to explore the problem space. Tests can be amended, and even deleted as you go along. It's a tight loop consisting of:

    - while not finished {
        - Invent a test, write it -- it is likely to not compile, as it will reference types and methods that don't exist
        - Write just enough code for the test to compile - skeleton classes with methods that return null
        - Code until all your tests pass
        - refactor to remove any duplicate code, re-running the test suite between each refactoring
    - }

    It is *not* a matter of writing all your tests upfront. It's more like "Hmm, my game is going to need a scoreboard class, and when I construct it, the score should be zero, so I'll write a test for that now."

Some people carve careers, others chisel them.

Working...