Wallaby.js Blog
Integrated Continuous Testing Tool for JavaScript

JavaScript tests cost efficiency

Everything has its price. Adding a new feature, maintaining an existing one, writing tests. When you are writing and maintaining tests in C# or Java, you are getting recorded and executable scenarios with your assumptions for that price. What about JavaScript, are you getting exactly the same?

Let’s imagine that in the code below, I have covered the “try” block in my tests, but didn’t bother covering “catch” block as I thought it is trivial and not worth testing.

var myService = { 
  'doWork': function() { throw new Error('error'); },
  'cancelWork': function() {},
  'handleError': function() {},
  'stoр': function() {}
};

try {
  myService.doWork();
}
catch (err) {
  myService.cancelWork();

  if (e.message) {
    myService.hanldeError();
  }
  
  myService.stop();
}

How many problems did I miss? Simple code editor will find nothing. Careful reader may find one or two issues. Intelligent code editor with nice static analysis and spell checker may find all three, though there are many cases when it won’t help either as static analysis capabilities in the dynamic world of JavaScript are limited. Test will find all three errors just as a free addition to its main duty.

So in case if you are still looking for more reasons to cover your JavaScript code, here is one more - JavaScript tests are more cost efficient than in C# or Java, because for the same price you are also getting some form of a compiler for your code.