pirple / The-NodeJS-Master-Class

Code samples for the Node.js Master Class
1.33k stars 1.21k forks source link

PR to improve tests by requiring callback fn only for async tests #10

Open jmaguirrei opened 6 years ago

jmaguirrei commented 6 years ago

Section 6 - Adding Unit Tests modified to make testing more flexible.

Synchronous tests like:

// Assert that the getANumber function is returning 1
unit['helpers.getANumber should return 1'] = function(/* done */){
  var val = helpers.getANumber();
  assert.equal(val, 1);
  // done();
};

Does not require callback function (done) anymore.

On the other hand, async tests still works when callback function is passed:

// Logs.list should callback an array and a false error
unit['logs.list should callback a false error and an array of log names'] = function(done){
  logs.list(true,function(err,logFileNames){
      assert.equal(err, false);
      assert.ok(logFileNames instanceof Array);
      assert.ok(logFileNames.length > 1);
      done();
  });
};

So test runner now is more flexible and reduces boilerplate for sync functions.