Chai: How to assert null with should

Created on 25 Jul 2012  Â·  8Comments  Â·  Source: chaijs/chai

should.be.null throw an error saying should.be doesn't have this null method.

How to transform the expect(null).to.be.null to should phrases?

Most helpful comment

Well, you have a couple of options...

var chai = require('chai')
  , should = chai.should()
  , example = null;

I am assuming you are trying the following and its not working...

example.should.be.null;

The should interface extends Object.prototype with the should keyword and since null is not an Object... etc.

Here are a few of the alternatives, given that you have var should = chai.should(); as opposed to just calling chai.should().

// will test example to be equal to either `null` or `undefined`.
should.not.exist(example);

// if you need exactly null
should.equal(example, null);

In most cases the not.exist will likely work for you, especially if you are testing async callbacks in node.

db.get('abc', function (err, doc) {
  should.not.exist(err);
  should.exist(doc);
  // ...
});

Hope that helps!

All 8 comments

Well, you have a couple of options...

var chai = require('chai')
  , should = chai.should()
  , example = null;

I am assuming you are trying the following and its not working...

example.should.be.null;

The should interface extends Object.prototype with the should keyword and since null is not an Object... etc.

Here are a few of the alternatives, given that you have var should = chai.should(); as opposed to just calling chai.should().

// will test example to be equal to either `null` or `undefined`.
should.not.exist(example);

// if you need exactly null
should.equal(example, null);

In most cases the not.exist will likely work for you, especially if you are testing async callbacks in node.

db.get('abc', function (err, doc) {
  should.not.exist(err);
  should.exist(doc);
  // ...
});

Hope that helps!

Hi, I guess should.equal(example, null) is the closest I can get for checking exactly null object. Thanks for this example. @logicalparadox

Anytime!

should.be.null(example)

looks nice, too.

I'm using chai in the browser, so I'm not requiring it with CommonJS. How do I test for undefined with the should style? I tried quite some stuff, but none worked.

/ping @logicalparadox

Once chai.js is included is a script I usually have a script block that sets up the should interface

<script>
  var should = chai.should();
</script>

And then include my tests. You can see a full example here.

Once you have done that you can simple do any of the following...

should.exist(something); // not undefed or null
should.not.exist(somethingElse); // undefined or null
should.equal(somethingElse, undefined);

The other thing I have seen is a quick mix of the expect interface.

request
.get('/some/url')
.end(function (err, res) {
  chai.expect(err).to.not.exist;
  res.should.have.property('body');
  // etc..
});

Let me know if you have further issues and I can help you trace them down.

Thanks for the quick reply. :) I could swear I tried chai.expect earlier. It works perfectly now.

Chai is awesome by the way! Can't wait to replace QUnit with it.

Am 30.11.2012 um 21:52 schrieb Jake Luer [email protected]:

Once chai.js is included is a script I usually have a script block that sets up the should interface

And then include my tests. You can see a full example here.

Once you have done that you can simple do any of the following...

should.exist(something); // not undefed or null
should.not.exist(somethingElse); // undefined or null
should.equal(somethingElse, undefined);
The other thing I have seen is a quick mix of the expect interface.

request
.get('/some/url')
.end(function (err, res) {
chai.expect(err).to.not.exist;
res.should.have.property('body');
// etc..
});
Let me know if you have further issues and I can help you trace them down.

—
Reply to this email directly or view it on GitHub.

Thanks for the kind words! I can't wait to hear about the switch!

Was this page helpful?
0 / 5 - 0 ratings