Skip to content

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

@npwork

Description

@npwork

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions