diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 959d210d7..61fa16c34 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -325,6 +325,29 @@ module.exports = function (chai, _) { ); }); + /** + * ### .defined + * + * Asserts that the target is not `undefined`. + * + * expect(undefined).to.be.defined; + * expect(null).to.not.be.defined; + * + * Needed because it is intuitive to invert .undefined, + * which would always pass if .defined is nonexistent. + * + * @name undefined + * @api public + */ + + Assertion.addProperty('defined', function () { + this.assert( + undefined !== flag(this, 'object') + , 'expected #{this} to be defined' + , 'expected #{this} not to be defined' + ); + }); + /** * ### .exist * diff --git a/test/expect.js b/test/expect.js index d504a8eaf..91dbf1720 100644 --- a/test/expect.js +++ b/test/expect.js @@ -70,6 +70,17 @@ describe('expect', function () { }, "expected '' to be undefined") }); + it("defined", function() { + expect({}).to.be.defined; + expect(function() {}).to.be.defined; + expect(false).to.be.defined; + expect(undefined).to.not.be.defined; + + err(function(){ + expect(undefined).to.be.defined; + }, "expected undefined to be defined"); + }); + it('exist', function(){ var foo = 'bar' , bar;