-
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
t.like not handling arrays as expected #2627
Comments
(Not a maintainer, but) This is behaving as documented: "Any values in It would sometimes be useful for One can achieve the behavior you expect either by writing one's own selector function and then using |
I had interpreted it as anything that was JSON serializable. But I could see them meaning it more literally. What do you mean about |
Internally, what (The selector function used by AVA is in lib/like-selector.js) So if you want to recurse into arrays, you could write a selector function something like: function selectWithArrays(actual, selector) {
if (isPlainObject(selector)) {
// ...
} else if (Array.isArray(selector) && Array.isArray(actual)) {
const selected = [];
for (const [index, value] of selector.entries()) {
selected.push(selectWithArrays(actual[index], value));
}
return selected;
} else {
return actual;
}
} and then use |
@ninevra is correct.
Ultimately a "partial equal" feature could be added there, allowing us to be smarter with say (primitive-value) sets and maps, but even then deciding how two arrays or complex-value sets are partially equal becomes difficult. (I'm closing this issue for housekeeping purposes, but let's keep the conversation going.) |
I was expecting it to just treat an array the way it treats and object. That is, element Not saying that is the only interpretation for arrays, but it is one way. |
Yea, I appreciate that. I'm not quite sure how we'd build that on top of Concordance though, especially with nested arrays. |
I have no experience with concordance, but could you not simply transform any arrays to their "equivalent" object before doing the comparison with the selector? |
You'd have to walk the object tree to find all those arrays. It's fine for simple cases but much harder for all cases. |
Not sure I see cases where it is hard, unless you are worried about it becoming slow for giant objects? You just need to recurse down the thing. |
Recursing is the problem. For example it has to work for sets and maps (keys included). |
I'm migrating tests from Jest to Ava and we use |
@novemberborn yeah I will try to create tests around it 👍 |
I am trying to use the
t.like
assertion on deep objects with arrays of objects in them.The following simplified example fails:
I expected the above assertion to pass.
It seems that elements within arrays need to be deep equal rather than "like" for the assertion to pass.
AVA version:
The text was updated successfully, but these errors were encountered: