Introduction To JavaScript Unit Testing

Testing JavaScript code is not just a matter of using some test runner

What is a unit anyway? In the best case, it is a pure function that you can deal with in some way — a function that always gives you the same result for a given input. This makes unit testing pretty easy, but most of the time you need to deal with side effects, which here means DOM manipulations. It’s still useful to figure out which units you can structure your code into and to build unit tests accordingly.

 

With that in mind, Jörn Zaefferer can obviously say that starting with unit testing is much easier when starting something from scratch. But that’s not what this article is about. This article is to help you with the harder problem: extracting existing code and testing the important parts, potentially uncovering and fixing bugs in the code. The process of extracting code and putting it into a different form, without modifying its current behavior, is called refactoring.

Comments

Be the first to write a comment

You must me logged in to write a comment.