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

Added handling for undefined and null on expect empty (fails on negate flag too) #475

Merged
merged 2 commits into from
Jun 30, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Member

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.

} else if (Array.isArray(obj) || 'string' === typeof obj) {
expected = obj.length === 0;
} else if ('object' === typeof obj) {
expected = Object.keys(obj).length === 0;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in my previous PR - just using Object.keys() seems to be the most optimum path, nicely.


this.assert(
!expected
expected
, 'expected #{this} to be empty'
, 'expected #{this} not to be empty'
);
Expand Down
29 changes: 29 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI the current behaviour is expect(0).to.be.empty passes. Can you please explain why you want it to now fail?

Copy link
Author

Choose a reason for hiding this comment

The 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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error messages for these feel very ambiguous - "expect undefined to be empty" is a semantic noodle scratcher, I'd prefer to see more specific error messages.


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(){
Expand Down