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

fix: toHaveProperty with expect.any (fix: #1675) #1682

Merged
merged 3 commits into from Jul 24, 2022

Conversation

skarab42
Copy link
Contributor

I'm not sure I implemented the fix in the right place, but it seems to fix the problem in #1675.

Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

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

Nope, you need to use equals function (already imported as jestEquals). It supports asymmetric matchers out of the box.

@skarab42
Copy link
Contributor Author

@sheremet-va Does jestEquals support dot notation?

expect(complex).toHaveProperty('bar.foo', 'foo')
expect(complex).toHaveProperty('bar.arr[0]')
expect(complex).toHaveProperty('bar.arr[1].zoo', 'monkey')
expect(complex).toHaveProperty('bar.arr.0')
expect(complex).toHaveProperty('bar.arr.1.zoo', 'monkey')
expect(complex).toHaveProperty(['bar', 'arr', '1', 'zoo'], 'monkey')
expect(complex).toHaveProperty(['foo.bar[0]'], 'baz')

@sheremet-va
Copy link
Member

@sheremet-va Does jestEquals support dot notation?

expect(complex).toHaveProperty('bar.foo', 'foo')
expect(complex).toHaveProperty('bar.arr[0]')
expect(complex).toHaveProperty('bar.arr[1].zoo', 'monkey')
expect(complex).toHaveProperty('bar.arr.0')
expect(complex).toHaveProperty('bar.arr.1.zoo', 'monkey')
expect(complex).toHaveProperty(['bar', 'arr', '1', 'zoo'], 'monkey')
expect(complex).toHaveProperty(['foo.bar[0]'], 'baz')

jestEquals compares two values. How you get them is only for you to decide.

@skarab42
Copy link
Contributor Author

jestEquals compares two values. How you get them is only for you to decide.

Is there any method already used in vitest? like lodash.get? If not, can I use lodash? Or do I have to do my implementation? Sorry for all the questions 😅

@sheremet-va
Copy link
Member

jestEquals compares two values. How you get them is only for you to decide.

Is there any method already used in vitest? like lodash.get? If not, can I use lodash? Or do I have to do my implementation? Sorry for all the questions 😅

I don't think we use it anywhere. You can check source code for this matcher in chai repo, maybe they have a public API for this things.

@skarab42
Copy link
Contributor Author

@sheremet-va I refactored using jestEquals and chai.util.getPathInfo. Thanks for pointing me to the solution ;)

@sheremet-va sheremet-va merged commit 35112e3 into vitest-dev:main Jul 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants