question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an assertion for same members where order is important

See original GitHub 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:

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:closed
  • Created 7 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
meebercommented, Jun 12, 2016

What should be the behavior if both the contains and the ordered flags are set? Specifically, should both of these tests pass or only the first?

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.

2reactions
meebercommented, Jun 4, 2016

Hmm… I think there are two main approaches:

  1. New flag that tells the members assertion to require same-order.
  2. New assertion that is very similar to members but requires same-order.

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
Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the importance of the order of the assertions in Z3?
I have two files whose content is identical except for the order in which I placed the assertions: in one file, the assertions...
Read more >
Testing Arrays and Objects with Chai.js | by Titus Stone
While eql compares content and enforces order, members only compares content allowing assertions that only care about values being present.
Read more >
Best practices for assertions
Whatever strategy you take for assertions in your application, make sure that you are on the same page as other members of your...
Read more >
More Assertions
This document will show you more assertions as well as how to construct complex failure messages, propagate fatal failures, reuse and speed up...
Read more >
Python's assert: Debug and Test Your Code Like a Pro
An important point regarding the assert syntax is that this ... Membership assertions allow you to check if a given item is present...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found