Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test error callback in Node.js

I'm using Mocha and Chai with a node project and wondering how do I test the error callback in node functions?

Here is an example of my code that I want to test:

  savePlayer: function(player) {

  var playerName =  player.name;

    modules.fs.writeFile('./Data/' + playerName + '.json', JSON.stringify(player), function (err) {
      if (err) {
        return console.log(err.message);
      }
    });
}

This is my test:

describe("savePlayer", function() {
  it("Should save the player in JSON, using thier name", function() {

     var player = {name: "test" }

    modules.data.savePlayer(player);

   var playerFile =  modules.fs.readFileSync('Data/test.json').toString('utf8');

    expect(playerFile).should.exist;

  });
});

This passes, but I want full code coverage. This line return console.log(err.message); is untested and I'm unsure how to fake an error and test that the console reported an error.

like image 433
Liam Kenneth Avatar asked Nov 30 '15 12:11

Liam Kenneth


1 Answers

Here's an annotated example using sinon and chai:

var fs         = require('fs');
var sinon      = require('sinon');
var expect     = require('chai').expect;
var savePlayer = require('your-module').savePlayer;

describe('savePlayer', function() {
  // We spy on `console.log()` calls. Spying means that calls to this function
  // are recorded, and we can check to see if it, for instance, was called with
  // particular arguments.
  var consoleSpy = sinon.spy(console, 'log');

  // We stub `fs.writeFile()`. Stubbing means that calls to this function 
  // are taken over, and we can determine exactly how it should act.
  var fsStub     = sinon.stub(fs, 'writeFile');

  // Before each test, the spy and stub are reset.
  beforeEach(function() {
    consoleSpy.reset();
    fsStub.reset();
  });

  // After _all_ tests, the original functions are restored.
  after(function() {
    consoleSpy.restore();
    fsStub.restore();
  });

  // Test #1: if `fs.writeFile()` fails, it should trigger a call to `console.log`.
  it('should log an error when `fs.writeFile` fails', function() {
    var msg = 'my test error';

    // Here we let `fs.writeFile()` call the callback with an error.
    fsStub.yields( new Error(msg) );

    // Call your function.
    savePlayer({ name : 'xx' });

    // Check to make sure that `console.log()` is called with the same error message.
    expect( consoleSpy.calledWith(msg) ).to.be.true;
  });

  // Test #2: when `fs.writeFile()` went okay, nothing should be logged.
  it('should not log an error when `fs.writeFile` is okay', function() {
    // As per Node convention, falsy first argument means 'no error'.
    fsStub.yields( null );

    // Call your function.
    savePlayer({ name : 'xx' });

    // We expect that `console.log()` wasn't called.
    expect( consoleSpy.called ).to.be.false;
  });

});
like image 130
robertklep Avatar answered Oct 01 '22 14:10

robertklep