Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
npwork opened this issue Dec 30, 2021 · 3 comments
Open

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

npwork opened this issue Dec 30, 2021 · 3 comments

Comments

@npwork
Copy link

npwork commented Dec 30, 2021

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

@JamieMason
Copy link
Owner

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.

@npwork
Copy link
Author

npwork commented Dec 30, 2021

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

@JamieMason
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants