Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Building on this tutorial testing an angularjs app with chai, I want to add a test for an undefined value using the "should" style. This fails:

it ('cannot play outside the board', function() {   scope.play(10).should.be.undefined; }); 

with error "TypeError: Cannot read property 'should' of undefined", but the test passes with the "expect" style:

it ('cannot play outside the board', function() {   chai.expect(scope.play(10)).to.be.undefined; }); 

How can I get it working with "should"?

like image 207
thebenedict Avatar asked Oct 06 '13 13:10

thebenedict


2 Answers

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'); }); 
like image 68
David Norman Avatar answered Oct 18 '22 20:10

David Norman


should.equal(testedValue, undefined); 

as mentioned in chai documentation

like image 38
daniel Avatar answered Oct 18 '22 19:10

daniel