assert.lengthOf {error: "Error", status: false}, 2 # => expected { Object (status, error) } to have a property 'length'
My workaround:
assert.equal Object.keys(myObj).length, 2
What about implementing something like above inside the lengthOf() that is used on objects to get the number of top-level properties?
Perhaps something more robust is needed, http://stackoverflow.com/questions/5223/length-of-javascript-object-ie-associative-array
First, it's a bit disrespectful to post bug reports in a language other than the one the project is written in. It'd be cool if you could rewrite your bug report for ChaiJS in... JS.
That said, I think this is error-prone and not very transparent. Objects don't have a canonical "length." E.g. what is the length of { length: 9001 }. You are actually asking what the length of the object's keys array is, and that would be done exactly as you describe:
assert.equal(Object.keys(myObj).length, 2);
Sorry. Didn't thought that two lines of coffee would offend, but here we go:
Problem:
assert.lengthOf({
error: "Error",
status: false
}, 2); // => expected { Object (status, error) } to have a property 'length'
Eventual solution:
assert.equal(Object.keys(myObj).length, 2);
I am well aware of the fact that objects doesn't have a "length" per se. I think most of us are.
However it is helpful, at least to me, to be able to get number of top-level props from an object easily. What do you think?
Well, I repeat my earlier question. What do you expect for the following cases:
var obj = { length: 9001 };
var notAnArray = document.querySelectorAll("div");
notAnArray.length = 100;
var hasNoOwnProperties = Object.create([1, 2, 3]);
However it is helpful, at least to me, to be able to get number of top-level props from an object easily. What do you think?
Personally, I think the code is a lot clearer for anyone reading it if you use the normal JavaScript way of getting the number of top-level properties of an object: Object.keys(obj).length. Otherwise, when reading the tests, you have to ask yourself which particular flavor of magic length-determination is used by Chai.
We'll see what @logicalparadox thinks though!
Changing the way lengthOf behaves to your suggestion adds a level of ambiguity that I am not comfortable with. I think many (myself especially) rely on lengthOf to throw if there is not a length property on whatever object or array I happen to be testing. I also find the Object.keys(obj).length solution much more descriptive than anything I can think of for core.
If you do this a lot and really want a method on the assert, Chai is extensible via helpers. Maybe add assert.objSize for your modules tests?
Most helpful comment
Sorry. Didn't thought that two lines of coffee would offend, but here we go:
Problem:
Eventual solution:
I am well aware of the fact that objects doesn't have a "length" per se. I think most of us are.
However it is helpful, at least to me, to be able to get number of top-level props from an object easily. What do you think?