Skip to content
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

A matcher that checks that two arrays contain the same elements #114

Closed
denizdogan opened this issue Mar 22, 2018 · 7 comments
Closed

A matcher that checks that two arrays contain the same elements #114

denizdogan opened this issue Mar 22, 2018 · 7 comments

Comments

@denizdogan
Copy link
Contributor

Feature Request

Description: A matcher that checks that two arrays contain the same elements. Examples:

expect([1, 2]).toMatchArray([2, 1]) // order doesn't matter!
expect([1, 2, 2]).toMatchArray([2, 1, 2]) // same number of 2s, all good
expect([1, 2]).not.toMatchArray([1, 2, 2]) // one extra 2, FAIL
expect([1, 2, 2]).not.toMatchArray([1, 2]) // one extra 2, FAIL

Possible solution: Given arrays A and B, check that they are equal in length. For each element in A, check that the element is equal to any element in B. If it is, remove the element from both arrays. Loop until both arrays are empty.

@mattphillips
Copy link
Member

Hey @denizdogan

I like the sounds of this matcher! I'm not too sure about the name though, as toMatchArray sounds like it would need to be identical in terms of order.

How about something like .toIncludeValues([values])?

jest-extended currently has similar APIs in .toIncludeAllMembers([members]) and .toIncludeAnyMembers([members]) which can be used for reference in terms of structuring the matcher.

How would you feel about sending a PR for this?

@denizdogan
Copy link
Contributor Author

@mattphillips Yeah, I'm not sure what to call it, .toIncludeValues isn't 100% accurate either, it sounds like that's the same as .toIncludeAllMembers to me! :)

Maybe .toEqualMembers? It's hard to describe the matcher concisely. Sure, I can make a PR for this!

@mattphillips
Copy link
Member

@denizdogan sorry missed the notification on this! I say lets just start a PR and we can decide on a name after 😆

@denizdogan
Copy link
Contributor Author

@mattphillips Yeah, I'll start working on the PR today or tomorrow, I just had a lot of things to take care of IRL 👍

@mattphillips
Copy link
Member

merged in #122

@trev-gulls
Copy link

Is this issue closed? I still have the following error in jest 26.6.3

Error: expect(received).toContainEqual(expected) // deep equality

Expected value: [0, 1]
Received array: [0, 1]

@AyobamiH
Copy link

AyobamiH commented Oct 21, 2022

I just started learning jest and I have been bashing my head against the keyboard before finding this, I still suffer from not being able to solve the issue off

Error: expect(received).toContainEqual(expected) // deep equality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants