-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add t.unorderedEqual()
assertion
#3234
base: main
Are you sure you want to change the base?
Conversation
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.
I think we could make this more generic, require both values to have an entries()
method that returns an Iterator
. Each result should then be an array with a key and a value.
You can make a map of the results for expected
and then iterate through actual
.
If we want we could then still require both values to have the same constructor
but it may be interesting not to.
docs/03-assertions.md
Outdated
@@ -137,6 +137,10 @@ Assert that `actual` is deeply equal to `expected`. See [Concordance](https://gi | |||
|
|||
Assert that `actual` is not deeply equal to `expected`. The inverse of `.deepEqual()`. Returns a boolean indicating whether the assertion passed. | |||
|
|||
### `.unorderedEqual(actual, expected, message?)` | |||
|
|||
Assert that all values in `actual` are in `expected`, returning a boolean indicating whether the assertion passed. |
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 clarify the differences with deepEqual
?
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.
Thoughts?
Assert that all values in
actual
are inexpected
. This is a variant of.deepEqual()
that does not depend
on the order ofactual
/expected
and only compares instances ofMap
s orSet
s/arrays.The size of
actual
andexpected
must be equal. ForMap
s, each key-value pair inactual
must be in
expected
, and vice-versa. ForSet
s/arrays, each value inactual
must be inexpected
.Returns
true
if the assertion passed and throws otherwise.
@@ -123,6 +123,9 @@ export type Assertions = { | |||
* indicating whether the assertion passed. | |||
*/ | |||
truthy: TruthyAssertion; | |||
|
|||
/** Assert that all values in `actual` are in `expected`, returning a boolean indicating whether the assertion passed. */ |
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.
Here too let's clarify the differences with deepEqual
.
lib/assert.js
Outdated
if (actualInfo.size !== expectedInfo.size) { | ||
fail(new AssertionError({ | ||
assertion: 'unorderedEqual', | ||
message: 'size must be equal', |
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.
I think this should be the message
argument. You can then do something like this:
Lines 332 to 333 in a39dab0
raw: {actual, expected}, | |
values: [formatDescriptorWithLabel('Values are deeply equal to each other, but they are not the same:', actualDescriptor)], |
lib/assert.js
Outdated
return false; | ||
} | ||
|
||
const comparedKeysResult = concordance.compare(actual.keys, expected.keys, concordanceOptions); |
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.
Did you mean:
const comparedKeysResult = concordance.compare(actual.keys, expected.keys, concordanceOptions); | |
const comparedKeysResult = concordance.compare(actual.keys(), expected.keys(), concordanceOptions); |
Of course that returns an iterator, which Concordance may walk over (can't recall), but the keys won't be in the same order.
If both values have the same size, then you probably don't need this?
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.
Agh - the joys of plain JavaScript.
If both values have the same size, then you probably don't need this?
Good catch, I think you're right as well.
Please re-open if you have a chance to return to this. |
@novemberborn I don't think I can reopen by myself. |
@tommy-mitchell done! |
Closes #3020
Adds a new assertion for testing equality in an unordered manner between two
Map
s or twoSet
s/Array
s:Todos:
concordance
Actual extends Map | Set | Array
)