This is not a big leap for humanity, but it’s a big leap for me. Today I officially tried doing TDD in JavaScript. The task was perfect: implement a widget that would render a simple markup language that had almost a 1:1 correspondence to HTML. The markup looked something like:
|F02|Hello world!|B|This is bold|/B| etc. There are always some gotchas (like unbalanced tags and alike), but the task was perfect for state-based testing.
I googled TDD and JavaScript and found jsUnit. It was amazingly simple to get started with. Just downloading and adding a reference to the library. The idea is that you put your code under test in one file, and you create an HTML-file that references jsUnit and your tested code. That file also contains your unit tests. This is very clearly documented on the framework’s site, so I won’t repeat it here. Then you load another HTML-file, which is the runner. You point out your HTML-file with the tests and of you go. That’s all there’s to it.
This is the first time I’ve tried doing this for JavaScript, so I have no references and cannot make any comparisons. However, I got a very positive impression:
- The framework is really easy to install.
- It mimicks’ JUnit very well (it has
setUp,tearDown, tests start withtest, and the basic asserts are the same) - The runner looks nice
To truly recommend jsUnit, I’d like to figure you how well it runs in batch mode, and what it can actually do. I’ve only tried basic asserts so far. Anyway, It’s a cool tool worth trying only for the fun of it.
If I keep using it and it proves inadequate, I’ll probably write a post like “Why jsUnit sucks”
