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

test(to-have-length): add optional chaining case #1295

Conversation

hekystyle
Copy link
Contributor

Hi,

I tried to implement optional chaning operator (?.) for to-have-length rule.

@G-Rath
Copy link
Collaborator

G-Rath commented Nov 26, 2022

thanks but this isn't quite equivalent since toHaveLength doesn't support undefined - in the test cases you've added this will result in a matcher error which is still arguably ok because the test fails like you'd expect, but it causes an incorrect result when combined with not i.e.

it('is not the expected length', () => {
  // this will pass even if x is undefined
  expect(x?.length).not.toBe(1);

  // this will fail if x is undefined
  expect(x).not.toHaveLength(1);
});

Given that I don't think it's worth supporting optional chaining here

@hekystyle
Copy link
Contributor Author

Oh, I see. At least let me add valid test case for expect(users[0]?.permissions?.length).toBe(1);.

@hekystyle hekystyle force-pushed the support-optional-chaining-for-to-have-length branch from 5303d3d to e65f97a Compare December 2, 2022 18:31
@hekystyle hekystyle changed the title feat(to-have-length): support optional chaining operator test(to-have-length): add optional chaining case Dec 2, 2022
@G-Rath G-Rath merged commit 42ce29f into jest-community:main Dec 16, 2022
@github-actions
Copy link

🎉 This PR is included in version 27.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

None yet

2 participants