-
-
Notifications
You must be signed in to change notification settings - Fork 698
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 an assertion for same members where order is important #717
Comments
@dominicbarnes Thanks for the issue! I agree. Testing each element individually, either manually or via iteration, is awkward and doesn't produce useful diffs. And using @lucasfcosta @keithamus Is there a graceful way to handle this situation that I'm missing? |
Thanks for the issue @dominicbarnes. What about adding an assertion which compares arrays items one by one taking order into account but without going through every one of the objects' keys? IMO this is a common enough use case. Also, you can use chai-things for other assertions related to Arrays. |
Hmm... I think there are two main approaches:
Either way, it should be usable with either deep or strict comparison. Brainstorming syntax: expect([1, 2, 3]).to.have.ordered.members([1, 2, 3]); // strict
expect([1, 2, 3]).to.have.deep.ordered.members([1, 2, 3]); // deep
expect([1, 2, 3]).to.have.orderedMembers([1, 2, 3]); // strict
expect([1, 2, 3]).to.have.deep.orderedMembers([1, 2, 3]); // deep |
Considering our current assertions I'd go with the first one. It seems more fluid and more similar to the english lanaguage. Adding a flag with the chainable word |
At first glance, I really like the idea of adding an I'd also be content with adding |
For the assert interface, it would be something like you've written: The only thing I'm a little uneasy about with the word "ordered" is the ease with which it could be misinterpreted as "sorted". But that might not be an issue. I'm still kinda expecting @keithamus to pop in here with some fancy way to use the existing API to fulfill this use case :D |
Hey y'all. Good discussion going on. Looks like we don't have any existing assertion to cover this. I'm happy with an
|
Starting this now! |
What should be the behavior if both the expect([1, 2, 3]).to.include.ordered.members([1, 2]);
expect([1, 2, 3]).to.include.ordered.members([2, 3]); I'm leaning toward only the first passing, as I suspect it's a more common use case to test that an array begins with an ordered subset. |
@meeber I agree with you. I think that most people would expect the second assertion to fail, it's not common to expect that an array has ordered elements in the middle or in the end of it, IMO it goes against the main goal of the flag for this issue. |
I've come to discover that it's not straightforward to test that an array has the same members, and that those members are in the expected order. Consider this base example:
All of the assertions around "members" all explicitly do not consider order, but it would be really convenient to be able to assert that too:
My use-case is that I have an internal list that I can retrieve. In one case I return the list unsorted. (so
sameMembers
works great) In the other case, I am sorting it in a specific way, so I want to assert that my sort worked as expected.I am using
deepEqual
now, but the member objects can be fairly big/deep and it's very inefficient to traverse these objects, especially since I have the direct object refs in my tests.Another option is that I can just use
strictEqual
on each item in the array, but that gets unwieldy quickly imo:The text was updated successfully, but these errors were encountered: