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

feat(expect): add toBeOneOf matcher #6974

Merged
merged 11 commits into from
Dec 20, 2024
Merged

Conversation

zirkelc
Copy link
Contributor

@zirkelc zirkelc commented Nov 27, 2024

Description

Following the discussion #6961 I'd suggest to add a new asymmetric matcher expect.oneOf() for better support of optional properties, which can often be undefined or null if not set.

test('optional properties can be null or undefined', () => {
  const user = {
    id: 1,
    firstName: 'John',
    middleName: undefined,
    lastName: 'Doe'
  }

  expect(user).toEqual({
    id: expect.any(Number),
    firstName: expect.any(String),
    middleName: expect.oneOf([expect.any(String), undefined]),
    lastName: expect.any(String),
  })
})

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented Nov 27, 2024

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6f21c89
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/67651ffefc62ea00084ee677
😎 Deploy Preview https://deploy-preview-6974--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to discuss if this is actually worth it in builtin, but thanks for kicking it off!

One thing I wasn't sure is how error diff works. Can you add a test cases for that? You can find examples at the bottom of jest-expect.test.ts using snapshotError.

@hi-ogawa hi-ogawa added the p2-to-be-discussed Enhancement under consideration (priority) label Nov 27, 2024
@zirkelc
Copy link
Contributor Author

zirkelc commented Nov 28, 2024

@hi-ogawa I added the snapshot tests

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Dec 1, 2024

I just found jest-extended provides toBeOneOf https://jest-extended.jestcommunity.dev/docs/matchers/toBeOneOf
Their example is only the assertion version, but they supports asymmetric version too. Maybe should we just align with this? 🤔

@zirkelc
Copy link
Contributor Author

zirkelc commented Dec 1, 2024

That's a good idea. Just to be clear, you mean adding expect().toBeOneOf([]) additionally to the asymmetric expect.oneOf([])?

@hi-ogawa hi-ogawa added p2-nice-to-have Not breaking anything but nice to have (priority) and removed p2-to-be-discussed Enhancement under consideration (priority) labels Dec 5, 2024
@sheremet-va
Copy link
Member

sheremet-va commented Dec 9, 2024

That's a good idea. Just to be clear, you mean adding expect().toBeOneOf([]) additionally to the asymmetric expect.oneOf([])?

Kind of. If you use expect.extend, then it will be added to both automatically, so just do it that way 😄

@hi-ogawa
Copy link
Contributor

I added a example of adding toSatisfy internally via expect.extend in #7022
Can you make it something similar for toBeOneOf? 🙏

@zirkelc zirkelc changed the title feat(expect): add expect.oneOf asymmetric matcher feat(expect): add toBeOneOf matcher Dec 10, 2024
@zirkelc
Copy link
Contributor Author

zirkelc commented Dec 10, 2024

I changed it to a custom matcher. Let me know if anything else should be changed.

{
"actual": "0",
"diff": "- Expected:
toBeOneOf<Any,,>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff doesn't handle null and undefined correctly. It should be:

Suggested change
toBeOneOf<Any,,>
toBeOneOf<Any,null,undefined>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is unfortunate. Maybe can we try stringify instead of String? 🤔

toAsymmetricMatcher() {
return `${this.toString()}<${this.sample.map(String).join(', ')}>`
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is better. But it also affects toSatisfy()

Comment on lines -735 to +907
"value": toSatisfy<(value) => value % 2 !== 0>,
"value": toSatisfy<[Function isOdd]>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It prints the function name instead of its implementation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is alright since this is how we normally format the function, so I guess we can say it's a better alignment.

Comment on lines -639 to +688
`[AssertionError: expected Error: 2 to match object { message: toSatisfy{…} }]`,
`[AssertionError: expected Error: 2 to match object { Object (message) }]`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this looks puzzling, but what's happening is we have truncation logic here for main error message and that just kicked in due to toSatisfy format got a bit longer. This is fine as well.

Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Comment on lines -735 to +907
"value": toSatisfy<(value) => value % 2 !== 0>,
"value": toSatisfy<[Function isOdd]>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is alright since this is how we normally format the function, so I guess we can say it's a better alignment.

Comment on lines -639 to +688
`[AssertionError: expected Error: 2 to match object { message: toSatisfy{…} }]`,
`[AssertionError: expected Error: 2 to match object { Object (message) }]`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this looks puzzling, but what's happening is we have truncation logic here for main error message and that just kicked in due to toSatisfy format got a bit longer. This is fine as well.

@hi-ogawa hi-ogawa merged commit 3d742b2 into vitest-dev:main Dec 20, 2024
13 checks passed
@zirkelc zirkelc deleted the feat-expect-oneof branch December 20, 2024 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Status: Approved
Development

Successfully merging this pull request may close these issues.

3 participants