Add an assertion for same members where order is important
See original GitHub issueI’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:
var a = {};
var b = {};
var list = [ a, b ];
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:
// no idea what to call this assertion
assert.sameExactMembers(list, [ a, b ]);
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.
// in my case, a & b could be huge, so would prefer not traverse them
assert.deepEqual(list, [ a, b ]);
Another option is that I can just use strictEqual
on each item in the array, but that gets unwieldy quickly imo:
// only 2 in this case... but it's copy-pasta and ugly beyond a few members
assert.strictEqual(list[0], a);
assert.strictEqual(list[1], b);
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (9 by maintainers)
Top GitHub Comments
What should be the behavior if both the
contains
and theordered
flags are set? Specifically, should both of these tests pass or only the first?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.
Hmm… I think there are two main approaches:
members
assertion to require same-order.members
but requires same-order.Either way, it should be usable with either deep or strict comparison.
Brainstorming syntax: