是否有忽略 jest.js 中元素位置的数组相等匹配函数? [英] Is there an Array equality match function that ignores element position in jest.js?
问题描述
我知道 .toEqual()
检查普通对象的所有字段的相等性:
I get that .toEqual()
checks equality of all fields for plain objects:
expect(
{"key1":"pink wool","key2":"diorite"}
).toEqual(
{"key2":"diorite","key1":"pink wool"}
);
这样就过去了.
但数组不是这样:
expect(["pink wool", "diorite"]).toEqual(["diorite", "pink wool"]);
在 jest 文档中似乎没有匹配器函数可以执行此操作,即测试两个数组的相等性,而不管它们的元素位置如何.我是否必须针对另一个数组中的所有元素测试一个数组中的每个元素,反之亦然?或者有其他方法吗?
There does not seem to be a matcher function that does this in the jest docs, i.e. that tests for the equality of two arrays irrespective of their elements positions. Do I have to test each element in one array against all the elements in the other and vice versa? Or is there another way?
推荐答案
没有内置的方法来比较数组而不比较顺序,但是你可以简单地使用 .sort()
在进行比较之前:
There is no built-in method to compare arrays without comparing the order, but you can simply sort the arrays using .sort()
before making a comparison:
expect(["ping wool", "diorite"].sort()).toEqual(["diorite", "pink wool"].sort());
您可以查看this fiddle中的示例.
这篇关于是否有忽略 jest.js 中元素位置的数组相等匹配函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!