-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
feat(chai): add containSubset #71972
base: master
Are you sure you want to change the base?
Changes from 12 commits
c71a520
f1a3da3
487ed38
dab1a3f
629f43f
fc60a07
484f2a4
6263efb
95cffdf
cefad68
d8d1cf2
1a4869b
c042421
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,6 +195,7 @@ declare global { | |
eq: Equal; | ||
eql: Equal; | ||
eqls: Equal; | ||
containSubset: ContainSubset; | ||
property: Property; | ||
ownProperty: Property; | ||
haveOwnProperty: Property; | ||
|
@@ -329,6 +330,10 @@ declare global { | |
(value: any, message?: string): Assertion; | ||
} | ||
|
||
interface ContainSubset { | ||
(expected: any): Assertion; | ||
} | ||
|
||
interface Property { | ||
(name: string | symbol, value: any, message?: string): Assertion; | ||
(name: string | symbol, message?: string): Assertion; | ||
|
@@ -523,6 +528,16 @@ declare global { | |
*/ | ||
deepStrictEqual<T>(actual: T, expected: T, message?: string): void; | ||
|
||
/** | ||
* Partially matches actual and expected. | ||
* | ||
* T Type of the objects. | ||
* @param actual Actual value. | ||
* @param expected Potential expected value. | ||
* @param message Message to display on error. | ||
*/ | ||
containSubset<T>(val: T, exp: Partial<T>, msg?: string): void; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double checking this, but this seems to only work for objects and a single depth. The implementation (https://github.com/chaijs/chai/blob/ce4c403d0f60818e1f5e1d4aa472e2a2b0a1ad8a/lib/chai/core/assertions.js#L4076) appears to actually support arrays, and the whole thing is deeply matched. That and Dates are not deeply matched. This makes me wonder if this needs some sort of DeepPartial or something to capture the full behavior (and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I was hoping to improve on the original typing but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not, no... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I simplified it to use |
||
|
||
/** | ||
* Asserts valueToCheck is strictly greater than (>) valueToBeAbove. | ||
* | ||
|
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.
How does this work? I don't see this in their repo: https://github.com/search?q=repo%3Achaijs%2Fchai-as-promised%20containSubset&type=code
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.
that works because
chai-as-promised
will iterate over all the available assertions dynamically and install new assertions based on that. It doesn't have a list of assertions that it maintains manually. This way, it will also work with assertions that come from plugins, for example. I have been usingchai-as-promised
withchai-subset
for years for exampleThere 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.
Are any of the other declarations here for addon packages?
It seems strange to declare that every addon exists, even if they aren't installed, or aren't supported (like for older versions of chai).
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.
containSubset
is not an add-on anymore, it was merged into chai proper, therefore adding it officially here now. What I was saying is that in the past I was using it as an add-on, and in that case I manually added the typings in my individual projectsThere 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.
So, yes, but I'm specifically talking about people using
chai<5.2.0
withchai-subset
andchai-as-promised
.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.
those people get the typing for free now?
not sure I get what's the concern...
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.
Sorry, misspoke; I meant
chai<5.2.0
andchai-as-promise
but withoutchai-subset
, so they'd see a declaration which wouldn't work because nothing provides it.It's probably unavoidable; it'd be nicer if the things tacked onto chai for promises were derived from the rest of chai, but I suspect that wouldn't actually work due to circularities without a major restructuring.
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.
well that'd be true of any assertion added to chai in the past or in the future, that's how the typings seem to have been developed?
for what it's worth, this update suggests that it requires chai>=5.2.0 by increasing the version too
one last point to consider is, chai devs have expressed interest in upstreaming chai-as-promised in the future as well
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.
Yeah. To be clear, I think this PR is probably fine, I'm just making sure that we do the right thing for an extremely popular set of packages.