Skip to content
This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
Denis Bardadym edited this page Jun 7, 2014 · 7 revisions

Some common tips

When testing with async calls (promises, callbacks etc).

E.g. with mocha you should call done callback argument with or without error. With last version of mocha you can return promise and mocha will evaluate it.

it('should call something', function test() {
  library.asyncCall(function(err, result) {
    should.fail(); 
   // this probably will not be called because test finished as soon `test` function finished
   // and assertion will look like not working
  })
})

it('should call something2', function test(done) {
  library.asyncCall(function(err, result) {
    should.fail(); 
   // in this case you will see test timeouts, again because done not called
  })
})

it('should call something3', function test(done) {
  library.asyncCall(function(err, result) {
    should.fail(); 
    done();
   // that is it, done called and mocha will wait for it call
  })
})

With promises it is similar:

// in this case again assertion will not happen because test will finished before .then callback called
it('should call something', function test() {
  library.asyncCall.then(function(result) {
    should.fail(); 
  })
})

// again you will see timeout
it('should call something2', function test(done) {
  library.asyncCall.then(function(result) {
    should.fail(); 
  })
})

// correct way
it('should call something3', function test(done) {
  //return required for promise evaluation
  return library.asyncCall(function(err, result) {
    should.fail(); 
  }).then(function() { done() }, done);
//first argument is wrapped `done` in case you return something in previous call
})

// correct way with last mocha
it('should call something4', function test() {
  //just return it and mocha will do rest
  return library.asyncCall(function(err, result) {
    should.fail(); 
  })
})

Using should without getter

When you call require('should'), Object.prototype extended with should getter. So

var should = require('should');
delete Object.prototype.should;

//use should that was returned

Confusing names

If you think that should is confusing word for invoking it as a functions, you always can use others like assert, expect, must etc.

var should = require('should');
var assert = should, expect = should;

assert.exist(null);

expect({ a: 1}).to.have.property('a');

Or

Object.defineProperty(Object.prototype, 'must', {
  get: function() { 
     return this.should;//or should(this);
  },
  enumerable: false 
});

({a :1)).must.have.property('a');
Clone this wiki locally