Many testing frameworks exist in JavaScipt nowadays.

The combination that I got hands on is the mocha [1] testing framework combined with the chai [2] assertion library, the sinon [3] mocking framework and the karma [4] test runner.

I thought to gather some resources here on how to work with these tools.

Click for part 1

Putting all together

Here is a more complex example:

var nextTask = 0, outOfOrder = false;
for (var i = 0; i < 1000; i++) {
  (function (i) { // Per task specific scope
    setTimeout(function task () {
      if (nextTask++ != i) {
        console.log("Out of order!", i, nextTask);
        outOfOrder = true;
  })(i);  // end of per task scope

if (!outOfOrder) {
  console.log("All functions were called in the order they were scheduled");

What is going on here: