JamieMason / JamieMason/expect-more

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

Open
#52 3 comments 0 reactions 0 assignees View on GitHub
Status: To Do Type: Feat
Dominant language
TypeScript
Stars
172
Forks
9
PR merge metrics
No merged PRs in 30d

Description

## 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)
```typescript
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}
Received: {"a": 10, "b": "qwerty"}

```
Maybe there is a way to check **optional** field?

## Suggested Solution

## Help Needed

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the TypeScript matcher entry points named in the example, toMatchObject and toBeOptionalOf, and inspect the existing matcher tests; no test file is identified in the issue. Done means an absent c field is accepted while a present c field is validated, without changing the required checks for a and b.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.