JamieMason/expect-more

Check optional fields (may be present in object or not)

npwork opened this issue · 3 comments

Description

I have a case when the field can be present or not in the object. I would like to check the field if it's present in the object
Case 1: { a: 10, b: 'qwerty' }
Case 2: { a: 10, b: 'qwerty' , c: 10}

(this won't work)

expect({ a: 10, b: 'qwerty' }).toMatchObject({
    a: expect.toBePositiveNumber(),
    b: expect.toBeNonEmptyString(),
    c: expect.toBeOptionalOf(expect.toBeNumber()),
  })

Returns error:

Expected: {"a": toBePositiveNumber<>, "b": toBeNonEmptyString<>, "c": toBeOptionalOf<toBeNumber>}
Received: {"a": 10, "b": "qwerty"}

Maybe there is a way to check optional field?

Suggested Solution

Help Needed

Hey Nick, I think this could be related to #51 in that expect-more-jest doesn't understand asymmetric matchers when it receives one as an argument. This needs to happen, but until that is released we will need to only provide regular values as arguments to matchers from this library unfortunately.

Can't wait for this feature to be implemented. You have an amazing library, keep going!

You're welcome Nick, thanks a lot. Receiving asymmetric matchers as arguments is a pretty big oversight I hadn't thought about, but it'll be do-able I'm pretty sure.