-
-
Notifications
You must be signed in to change notification settings - Fork 696
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
Added handling for undefined and null on expect empty (fails on negate flag too) #475
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -376,14 +376,16 @@ module.exports = function (chai, _) { | |
var obj = flag(this, 'object') | ||
, expected = obj; | ||
|
||
if (Array.isArray(obj) || 'string' === typeof object) { | ||
expected = obj.length; | ||
} else if (typeof obj === 'object') { | ||
expected = Object.keys(obj).length; | ||
if (obj == null) { | ||
expected = flag(this, 'negate'); | ||
} else if (Array.isArray(obj) || 'string' === typeof obj) { | ||
expected = obj.length === 0; | ||
} else if ('object' === typeof obj) { | ||
expected = Object.keys(obj).length === 0; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in my previous PR - just using |
||
|
||
this.assert( | ||
!expected | ||
expected | ||
, 'expected #{this} to be empty' | ||
, 'expected #{this} not to be empty' | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -397,6 +397,35 @@ describe('expect', function () { | |
err(function(){ | ||
expect({foo: 'bar'}).to.be.empty; | ||
}, "expected { foo: \'bar\' } to be empty"); | ||
|
||
err(function(){ | ||
expect(0).to.be.empty; | ||
}, "expected 0 to be empty"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI the current behaviour is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure about this when I added it, and kind of put it here for discussion. It was more because the empty seemed like a means to check something has no properties or length. Happy to remove that though if its deemed ok for numbers to pass. |
||
|
||
err(function(){ | ||
expect(null).to.be.empty; | ||
}, "expected null to be empty"); | ||
|
||
err(function(){ | ||
expect(undefined).to.be.empty; | ||
}, "expected undefined to be empty"); | ||
|
||
err(function(){ | ||
expect().to.be.empty; | ||
}, "expected undefined to be empty"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error messages for these feel very ambiguous - |
||
|
||
err(function(){ | ||
expect(null).to.not.be.empty; | ||
}, "expected null not to be empty"); | ||
|
||
err(function(){ | ||
expect(undefined).to.not.be.empty; | ||
}, "expected undefined not to be empty"); | ||
|
||
err(function(){ | ||
expect().to.not.be.empty; | ||
}, "expected undefined not to be empty"); | ||
|
||
}); | ||
|
||
it('property(name)', function(){ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not ideal - setting the negate flag will apply to the rest of the chain. Better to do a simple type check at the beginning and error out early.