Unit testing Javascript
Some say that it is not possible, some say that it is not worth, some say that it is madness, but doesn’t matter of what this “some” say we need to test our Javascript.
Testing javascript is not just about putting your automated test on place and running it. Sure that there you are also testing the javascript, but not unit testing it.
That kind of test is looking at the interaction of the javascript functions with the other elements, the unit test should look at it in small blocks, functions; you know the normal unit testing stuff.
But why it is so difficult to unit test javascript, well in fact it is not – the biggest problem is in which environment will we run our test? Opening a browser and pressing a button doesn’t look a synonym for automated test.
As everything in the IT world, you will find some open source projects to do it for you. They are running environments for you unit testing, some of them can even allow you to specific which “browser” you want to run the test against.
For those interested in it, here goes a list of frameworks that target unit testing javascript code:
- QUnit – https://github.com/jquery/qunit;
- Jasmine – http://pivotal.github.com/jasmine/ (it is a Behavior-drive framework);
- Jarvis – http://jarvis.tmont.com/; and
- jfUnit – http://felipenmoura.org/projetos/jfunit/home.php.
Sure that there is more than those, but it is already a good start point. So next time that someone tells you that you need to walk through fire and flames to unit test javascript code, you can say: “Nops dude I have the perfect cooling system”.
And as you don’t need to walk through fire and flames, I finish here with Through The Fire And Flames, from Dragonforce.
This one can be useful too 😉
http://jsunity.com/
Thanks Tiago! that is an interesting framework too. The context-agnostic perspective really fits frameworks like Node.js.
Next step is to check which one has the best integration possibilities in a automated test environment.