Archive for the ‘JavaScript’ Category

I dare say that the people who are kind enough to read this blog already know that I am a little freakily fanatic about unit testing. Believe it or not I do have a lot of experiences in which having unit tests in the code I was modifying saved me from doing very stupid and embarrassing mistakes and I want to share a very recent, although small, example.

For the past month I have been getting into Single Page Application development, better known as SPA and I find this book to be quite insightful. Anyway, when I read technical books I like to code all the examples and make sure they run as the book claims they do, I find I learn better that way. On the very first one, after coding a very large portion of JavaScript, I fired up the browser and … well nothing happened. I looked at the browser’s console and there were no errors whatsoever. To make a long story short I spent almost an hour reviewing and comparing the code to the book line by line, even with the downloaded code which worked. File comparing tools were not helping at all and it was a quite frustrating hour.

Finally I realized I had a typo in the following line:

var result = modal.$container.height === newConfig.heigt;

It was so subtle! Of course the newConfig object did not have a “heigt” property so its value was undefined, which of course is different that whatever value the height property in the container had and so the result was always false.

Of course I do not pretend that we should be writing unit tests for the code we copy from technical books. My point is that in a real project this kind of errors in a language like JavaScript could not only happen but cause to waste a lot of debugging time. All this time would be better spent writing a unit test that would immediately fail and point you in the right direction so the typo is found quickly and corrected, before it even reaches QA, UAT or, God forbids, production.

Because of what’s going on in the software development industry with things like mobile development and SPAs, JavaScript is gaining a lot of importance so we better start to get used to the idea, not only that we’ll be coding a lot more with this language but that we need to be concerned at unit testing that code too.