Chai: how to test for undefined with 'should' syntax

This is one of the disadvantages of the should syntax. It works by adding the should property to all objects, but if a return value or variable value is undefined, there isn't a object to hold the property.

The documentation gives some workarounds, for example:

var should = require('chai').should();
db.get(1234, function (err, doc) {
  should.not.exist(err);
  should.exist(doc);
  doc.should.be.an('object');
});

should.equal(testedValue, undefined);

as mentioned in chai documentation


(typeof scope.play(10)).should.equal('undefined');

Test for undefined

var should = require('should');
...
should(scope.play(10)).be.undefined;

Test for null

var should = require('should');
...
should(scope.play(10)).be.null;

Test for falsy, i.e. treated as false in conditions

var should = require('should');
...
should(scope.play(10)).not.be.ok;

I struggled to write the should statement for undefined tests. The following doesn't work.

target.should.be.undefined();

I found the following solutions.

(target === undefined).should.be.true()

if can also write it as a type check

(typeof target).should.be.equal('undefined');

Not sure if the above is the right way, but it does work.

According to Post from ghost in github