Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds assertions .to.be.defined #440

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
11 changes: 11 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down