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

to.have.property not detects undefined fields #380

Closed
sonicoder86 opened this issue Feb 27, 2015 · 2 comments
Closed

to.have.property not detects undefined fields #380

sonicoder86 opened this issue Feb 27, 2015 · 2 comments

Comments

@sonicoder86
Copy link

The property matcher behaves not consistently for undefined values

var obj = { x: undefined, y: 1}
expect(obj).to.have.property('x') // not pass

but checking like this:

expect(Object.getOwnPropertyNames(obj)).to.contain('x') // pass
@keithamus
Copy link
Member

Hey @BlackSonic thanks for the issue.

This issue is actually a duplicate of #184 - which was fixed in Chai 2.0.0. It was fixed in PR #308 if you're curious.

Here's a code sample of it working in Chai 2.0.0:

> var obj = { x: undefined, y: 1}
undefined
> expect(obj).to.have.property('x')
{ __flags: { ssfi: [Function], object: undefined, message: undefined } }

Also, if you need the behaviour to work for older version of chai, you can opt to use the ownProperty assertion instead, I believe.

expect(obj).to.have.ownProperty('x')

ownProperty doesn't pass the property value over though, so you cannot chain it in the same way.

I'm going to close this issue - because of the above. If you still feel it is a problem, let me know and I'll reopen it 😄

@sonicoder86
Copy link
Author

thanks, upgrading to new version solved the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants