【问题标题】:Using Jest matchers in .toMatchObject在 .toMatchObject 中使用 Jest 匹配器
【发布时间】:2021-11-30 19:29:34
【问题描述】:

这个问题是Include toBeCloseTo in Jest .toMatchObject的一般情况。

如何在.toMatchObject(object) 匹配器中使用任何Jest extend matcher

例如,测试一个对象有几个字段,包括一个字段foo > 零:

expect({ a: 'b', c: 'd', foo: 3 }).toMatchObject({
  a: 'b',
  c: 'd',
  foo: expect.toBeGreaterThan(0),  // expect.toBeGreaterThan is not a function
});

我知道我可以写expect(theObject.foo).toBeGreaterThan(0),但是有没有办法不从.toMatchObject 调用中分离出测试.foo 的逻辑?

【问题讨论】:

  • 您之前收到的答案中的技术是否不适用于所有其他非对称可用的匹配器?对于“一般情况”,您还希望得到什么?

标签: jestjs


【解决方案1】:

我知道,很晚了。结合 toBeOneOftoBePositive 为我工作:

expect({ a: 'b', c: 'd', foo: 3 }).toMatchObject({
  a: 'b',
  c: 'd',
  foo: expect.toBeOneOf([0, expect.toBePositive()]),
});

【讨论】:

    猜你喜欢
    • 2019-04-21
    • 2020-07-27
    • 2022-08-18
    • 2019-01-08
    • 2021-08-27
    • 2021-06-12
    • 1970-01-01
    • 2020-10-19
    • 2021-04-02
    相关资源
    最近更新 更多