【问题标题】:checking if two collections have a common member with jest检查两个集合是否具有与 jest 相同的成员
【发布时间】:2017-11-27 15:46:40
【问题描述】:

我想要一个测试数组是否包含特定对象的期望表达式。

我可以用这段代码做到这一点:

expect(insensitiveKeys.some(val => resultKeys.indexOf(val) !== -1)).toBe(false)

它检查 resultKeys 中的任何实例是否在 insensitiveKeys 中。

但我想要一个开玩笑的方法来做到这一点。

感谢您的帮助。

【问题讨论】:

    标签: javascript arrays jestjs


    【解决方案1】:
    let arr = [1, 2, 3, 4];
    let arr1 = ['apple','orange',1]
    
    let commonItems = arr1.filter((item) => {return arr.indexOf(item) != -1});
    

    commonItems 将返回 [1]

    您可以在期望语句中使用 commonItems.length。

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 2023-04-04
      • 1970-01-01
      • 2014-02-03
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多