user1080425 user1080425 - 2 years ago 247
Node.js Question

How to assert not null?

I'm very new in javascript testing, I would like to know how to assert not null in Mocha framework.

Answer Source

Mocha supports any assertion library you want. You can take a look at how it deals with assertions here: I don't know which one you want to use.

Considering you are using Chai, which is pretty popular, here are some options:

Consider "foo" to be the target variable you want to test


var assert = chai.assert;
assert(foo) // will pass for not null and not undefined
// or
assert(foo != null)
// or
assert.notEqual(foo, null);

In case you want to use assert, you don't even need Chai. Just use it. Node supports it natively:


var should = require('chai').should();
should.exist(foo); // will pass for not null and not undefined
// or
should.not.equal(foo, null);


var expect = chai.expect;
// or
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download