-
-
Notifications
You must be signed in to change notification settings - Fork 701
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add propertyDescriptor
assertion
#408
Conversation
@ljharb thanks for the PR! I'm not entirely sure if having Maybe it'd be better to have a generic property to get access to the descriptor interface, something like: expect(myObject).to.have.propertyDescriptor('length').and.have.property('enumerable', true); Slightly longer but much more flexible. Thoughts? |
Interesting - I do see how that'd be more flexible, but it's much more verbose - I want to use this in the es6-shim where I have lots of tests that assert that a property exists and is (or is not) enumerable. I don't often check configurability or writability, nor the existence of getters and setters. How complex would your suggestion be to add? I'm not sure I yet know enough about the internals of chai to be able to add it quickly. |
If we were to go with my suggestion (I'm not mandating it, just giving some food for thought) then you're about half way there... you'd need something like: function propertyDescriptor (name, msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object');
var descriptor = Object.getOwnPropertyDescriptor(Object(obj), name);
flag(this, 'object', descriptor);
this.assert(
descriptor
, 'expected #{this} to have descriptor ' + _.inspect(name)
, 'expected #{this} not to have have descriptor ' + _.inspect(name)
);
} Take particular note of |
I'd also say, either way, we should probably also make the assertion deep aware, so that developers could do |
Just to play devils advocate some more, you could use existing assertions in a fairly non-verbose way (and a way closer to how the API would be consumed) to check non-enumerability: it('should have non enumerable length', function () {
expect(object).to.not.include.keys('length');
expect(object).to.have.property('length');
}); |
The failure message for "not to include keys" isn't very clean though, since it would list all the keys iirc. I'll play with this and see what I come up with, thanks. |
@keithamus For the "deep" part - does that mean that |
The expect({ foo: bar: new Thing() }).to.have.deep.propertyDescriptor('foo.bar.length'); We could of course call it expect(new Thing()).to.have.ownPropertyDescriptor('length'); |
a75fdfa
to
334e579
Compare
@keithamus I've updated the PR to cover the generic propertyDescriptor assertion. I didn't add anything for "deep" because I don't know what that entails :-) |
enumerableProperty
assertionpropertyDescriptor
assertion
Also, as an aside, I'm not sure we need |
I copied the style verbatim from |
Yup, I see. I'll eat my words then, havePropertyDescriptor can stay. What are your thoughts about renaming them both to P.S. the keywords in chai are entirely optional |
Sure, it's |
@keithamus hmm - if I were to add |
Hmm, that is indeed a quandary. If es6/7 is to get My feelings are we should do the simplest thing first - so adding |
ES6 absolutely doesn't have that, and as a member of TC39 I haven't seen any proposals for it for ES7. I'm fine with calling it |
334e579
to
47c99ab
Compare
Just pushed an update with |
* | ||
* Asserts that the target has an own property descriptor `name`, that optionally matches `descriptor`. | ||
* | ||
* expect('test').to.have.ownPropertyDescriptor('length'); |
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.
Could you please add a few more examples here? Specifically I'd like to see the following:
expect('test').to.have.ownPropertyDescriptor('length', { enumerable: false, configurable: false, writable: false, value: 4 });
expect('test').ownPropertyDescriptor('length').to.have.property('enumerable', false);
expect('test').ownPropertyDescriptor('length').to.have.keys('value');
Feel free to adjust those as you see fit - but it should show a couple of examples to demonstrate what's possible (especially for any extra arguments).
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.
Sure, those look great :-)
5e1e680
to
8b6a3cc
Compare
Sweet! Awesome work @ljharb. Consider this merged 😄 |
Add `propertyDescriptor` assertion
No description provided.