I'm testing that an object matches a set of fields, but one of them is floating point and I need to use .toBeCloseTo. How can that be done within one expect?
expect(foo).toMatchObject({
bar: 'baz',
value: ???.toBeCloseTo(5), // TODO
});
I could use expect(foo.value).toBeCloseTo(5)
, but I don't want to break the logic into multiple expect
s, one for each floating point number.
Issue
The docs for
toMatchObject
states "You can match properties against values or against matchers".Unfortunately,
toBeCloseTo
is not currently available as an asymmetric matcher, it looks like these are the only asymmetric matchers currently provided by Jest.Solution
If you are using Jest v23 or higher you can create your own, essentially duplicating
toBeCloseTo
usingexpect.extend
:Note that
expect.extend
creates a matcher that can be used within functions liketoMatchObject
only in Jest v23 and higher.Alternate Solution
From this post by a Jest collaborator: "Although it is implied but not currently documented, Jest assertions evaluate asymmetric matcher objects as defined in Jasmine".
An asymmetric matcher using the logic from
toBeCloseTo
can be created like this: