Exist assertion on the assert interface

This commit is contained in:
lucasfcosta 2016-08-26 21:16:35 -03:00
parent e731740d5c
commit 374f405527
2 changed files with 67 additions and 0 deletions

View file

@ -460,6 +460,48 @@ module.exports = function (chai, util) {
new Assertion(val, msg).not.to.be.NaN; new Assertion(val, msg).not.to.be.NaN;
}; };
/**
* ### .exists
*
* Asserts that the target is neither `null` nor `undefined`.
*
* var foo = 'hi';
*
* assert.exists(foo, 'foo is neither `null` nor `undefined`');
*
* @name exists
* @param {Mixed} value
* @param {String} message
* @namespace Assert
* @api public
*/
assert.exists = function (val, msg) {
new Assertion(val, msg).to.exist;
};
/**
* ### .notExists
*
* Asserts that the target is either `null` or `undefined`.
*
* var bar = null
* , baz;
*
* assert.notExists(bar);
* assert.notExists(baz, 'baz is either null or undefined');
*
* @name notExists
* @param {Mixed} value
* @param {String} message
* @namespace Assert
* @api public
*/
assert.notExists = function (val, msg) {
new Assertion(val, msg).to.not.exist;
};
/** /**
* ### .isUndefined(value, [message]) * ### .isUndefined(value, [message])
* *

View file

@ -344,6 +344,31 @@ describe('assert', function () {
}, "expected 'hello' not to be NaN"); }, "expected 'hello' not to be NaN");
}); });
it('exists', function() {
var meeber = 'awesome';
var iDoNotExist;
assert.exists(meeber);
assert.exists(0);
assert.exists(false);
assert.exists('');
err(function (){
assert.exists(iDoNotExist);
}, "expected undefined to exist");
});
it('notExists', function() {
var meeber = 'awesome';
var iDoNotExist;
assert.notExists(iDoNotExist);
err(function (){
assert.notExists(meeber);
}, "expected 'awesome' to not exist");
});
it('isUndefined', function() { it('isUndefined', function() {
assert.isUndefined(undefined); assert.isUndefined(undefined);