Using Jest matchers in .toMatchObject

2019-06-05 21:49发布

This question is the general case for Include toBeCloseTo in Jest .toMatchObject.

How can I use any Jest extend matcher within the .toMatchObject(object) matcher?

For example, testing that an object has several fields, including a field foo > zero:

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

I know I could write expect(theObject.foo).toBeGreaterThan(0), but is there away to not break away the logic for testing .foo from the .toMatchObject call?

标签: jestjs
0条回答
登录 后发表回答