-
-
Notifications
You must be signed in to change notification settings - Fork 714
fix: allow empty keys assertion when target object is empty #1753
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
base: main
Are you sure you want to change the base?
Conversation
test/issue-1384.js
Outdated
| import {expect} from '../index.js'; | ||
|
|
||
| describe('Fix #1384: Empty keys support', function () { | ||
| it('should accept empty keys when the object is empty', function () { |
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.
these should be added to the existing expect tests, either as part of the existing test case or just move these two new ones and reword them
| "integrity": "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==", | ||
| "dev": true, | ||
| "license": "MIT", | ||
| "peer": true, |
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.
can you discard these changes? it'll be newer npm trying to "fix things up"
we can do it another time
|
this doesn't seem right yet the fix basically means we allow an empty set of keys if the input is also empty. but what if, unexpectedly, it isn't? then we'll get an error about not passing keys? even though the actual problem is that we expected the object to be empty and it wasn't currently, if we were to allow empty sets of keys, we'd hit this: chai/lib/chai/core/assertions.js Lines 2541 to 2561 in d63c74e
the so i suspect the real fix is something like: if (!keys.length && !every) {because if it is empty, and we're using |
|
@43081j Thanks for the review! I have updated the PR with the requested changes:
Ready for another look! |
|
did you forget to push? i can see you moved the tests but can't see the other changes there |
|
@43081j I apologize for the oversight! You are absolutely right—I missed pushing the logic updates in the previous commit. I have now verified and pushed the correct changes. The // Only throw if keys are empty AND we are NOT checking 'all'
if (!keys.length && !all) { |
|
@43081j Sir please check. |
lib/chai/core/assertions.js
Outdated
| // Fix for PR #1384: Allow empty keys only if we are checking 'all'. | ||
| // 'any' keys requires at least one key to be checked against. |
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.
| // Fix for PR #1384: Allow empty keys only if we are checking 'all'. | |
| // 'any' keys requires at least one key to be checked against. | |
| // Allow empty keys if we're checking `all` since you can assert it has _no keys_ | |
| // For `any`, we need at least one key. |
since the PR isn't relevant info anymore at this point (its a feature rather than a bug)
|
did you run a formatter? the diff is huge now 😬 if you can narrow it back down to the tests you added, we're good to go |
|
@43081j Thanks for catching that! I've reverted the accidental formatting changes, so the diff is clean now. I also updated the code comments as suggested to explain the logic better. Ready for the final review! If everything looks good, please feel free to merge. |
|
the test file is still hugely changed thanks to the formatter and now we seem to have lost |
|
@43081j , Sorry for early mistakes. I am new to open source and I have made changes according to the requirements please review it. Thank you for your feedbacks. |
Description
This PR fixes the issue where expecting empty keys on an empty object would throw "keys required".
It allows:
expect({}).to.have.all.keys([])to pass.History
This is a manual revival of PR #1384 by @Zirak. I have reapplied the logic to the latest codebase and added a regression test.
Closes #1384