-
-
Notifications
You must be signed in to change notification settings - Fork 693
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
Conversation
} else if (typeof obj === 'object') { | ||
expected = Object.keys(obj).length; | ||
if (obj == null) { | ||
expected = flag(this, 'negate'); |
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.
Overall some good work @Daveloper87 - but see my comments above. I think in summary, I see the assertion for Assertion.addProperty('empty', function () {
var obj = flag(this, 'object');
new Assertion(obj).to.exist;
this.assert(
Object.keys(obj) === 0
, 'expected #{this} to be empty'
, 'expected #{this} not to be empty'
);
}); This should assert first that the object is not null or undefined - after which it will look at the keys to ensure they are 0. |
Thanks for your comments, The lengthOf approach and the approach you have outlined in your comment looks a lot cleaner. Was not sure if it was ok to use other assertions, but looking at everything again it seems like its used throughout. So that looks good to me. My only question is around Object.keys(). Is chai 4.x.x only supporting ES6? because |
Using other assertions will probably one day not be ideal - but don't worry about that for now, just follow existing code conventions. Do you have evidence about ES6 Object.keys? Without offending I'd be surprised if ES6 makes breaking changes, so I'm somewhat doubtful of this. Looking at the spec[1] it seems to use |
Sorry, I did not explain that well. Object.keys('hello') would fail in browsers that do not currently have support for ES6. This will include node (without harmony). If you try that in node console or a browser like Safari, you should see a TypeError |
Ah, ES5 Object.keys is missing var obj = 'hello';
Object.keys(Object(obj)); // ["0", "1", "2", "3", "4"]
var obj = [1, 2, 3];
Object.keys(Object(obj)); // ["0", "1", "2"]
var obj = 42;
Object.keys(Object(obj)); // [] Thoughts? |
Ah great! Likewise! I have pushed a new commit based on your comments. numbers and functions will be classed as empty, but I guess if you expected an empty array and got a function, you have bigger problems... |
Great work @Daveloper87 😄. It'll be a while before the 4.x.x branch gets merged - #457 is our roadmap for now, which we're trying hard to focus on (namely modularising the chai codebase). If you've caught the PR bug then feel free to take a look at some of those and submit some PRs, they'd be very welcome! |
Added handling for undefined and null on expect empty (fails on negate flag too)
@keithamus Great! I will have a look over the coming weeks and try to dig in :) |
@keithamus Is chai 4.x.x going to be focused towards ES6? If so I can use Object.keys() to coerce primitives.
Also there is the option to do this:
Which would cover any new objects with length, but it would add support for checking if a function takes parameters or not (not sure that's what you would want from an empty check..).
Just an idea. I will wait for your feedback.