Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

introduction to unit-testing in javascript

I have never done any testing in javascript. I know, I know. But the reason is that I've never built big javascript applications before, so I've never seen any reason for getting into testing.

But now I figured it's about time to get crackin'.

The only problem is that everywhere I go, every testing framework seems to rely on the fact that people already know how to test with javascript, they only focus on why their testing framework is better than the next.

What I would like, is a very basic introduction to testing with javascript. When is it neccessary? What should you test? How should the tests be set up? How often do you test? You know, just very, very basic stuff.

So any links to texts or videos will be highly appreciated (:

Thanks.

edit: Just to make clear: What I'm looking for is introductions to testing, not specific frameworks. Because right now, I don't even know why I should test...

And if there are any books on the subject, that would be even better.

2nd edit: I found a really nice video from Nicholas Zakas on Yahoo! Theatre, where he explains TDD practice for javascript first, then explains how to use YUI testing to achieve those goals.

like image 729
peirix Avatar asked Sep 23 '09 10:09

peirix


1 Answers

Unit testing in general allows you to build up a battery of small tests that verify fine-grained bits of your code, especially edge cases. This is especially useful in Javascript where your application needs to run the same way on different browser platforms.

Creating a suite of such tests allow you to ensure that changes you make today don't break code you wrote yesterday (or a month ago).

For example, you may have a part of your application that walks all the DOM nodes in the document to find and bind to nodes it cares about. You decide to optimize this, perhaps by using the jquery selector. If you have a test that all the possible nodes can be found properly, you can quickly see if the changes you just made broke anything, on any of your target browsers.

You can also "fake out" XmlHttpRequest interactions with the server using various frameworks - this allows you to verify that your client code can react properly to all sorts of results and errors coming back from your backend.

Basically, as with other languages, unit tests in JS allow you to automate answering the question "did I just break anything" with these changes?

like image 182
levik Avatar answered Oct 19 '22 22:10

levik