是否存在忽略jest.js中元素位置的数组相等匹配函数? [英] Is there an Array equality match function that ignores element position in jest.js?

查看:108
本文介绍了是否存在忽略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"}
);

这样就过去了.

但是对于数组却不是这样:

But the same is not true for arrays:

expect(["pink wool", "diorite"]).toEqual(["diorite", "pink wool"]);

在玩笑的文档中似乎没有匹配器函数可以做到这一点,即测试两个数组的相等性,无论其元素位置如何.我是否必须将一个数组中的每个元素与另一个数组中的所有元素进行测试,反之亦然?还是有另一种方法?

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?

推荐答案

没有内置方法可以在不比较顺序的情况下比较数组,但是您可以使用

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());

您可以在此小提琴中查看示例.

这篇关于是否存在忽略jest.js中元素位置的数组相等匹配函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆