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

Document how expect().toHaveProperty works with a dot in the property name. #5653

Closed
hawkeye64 opened this issue Feb 24, 2018 · 6 comments · Fixed by #5668
Closed

Document how expect().toHaveProperty works with a dot in the property name. #5653

hawkeye64 opened this issue Feb 24, 2018 · 6 comments · Fixed by #5668

Comments

@hawkeye64
Copy link

Do you want to request a feature or report a bug?
bug

What is the current behavior?
toHaveProperty fails with nested property that has a dot (".") in the name.

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

Here is the data. It was originally xml from a soap message, then converted to json via package xml2js.
let response = {"AccessPolicyConfig": "false", "DefaultAccessPolicy": "true", "Dot1X": "false", "HttpDigest": "false", "KerberosToken": "false", "OnboardKeyGeneration": "false", "RELToken": "false", "RemoteUserHandling": "false", "SAMLToken": "false", "SupportedEAPMethods": "0", "TLS1.0": "false", "TLS1.1": "false", "TLS1.2": "false", "UsernameToken": "true", "X.509Token": "false"}

These tests fail:
expect(response).toHaveProperty('TLS1.0')
expect(response).toHaveProperty('TLS1.1')
expect(response).toHaveProperty('TLS1.2')
expect(response).toHaveProperty('X.509Token')

REPL.IT example:
https://repl.it/@hawkeye64/dot-in-property-test-fail

What is the expected behavior?
The have expect().toHaveProperty() to not fail.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

Jest v22.1.2 node v7.4.0 linux/amd64

config.json:
{
"testRegex": ".*-test\.js$",
"testEnvironment": "node"
}

@thymikee
Copy link
Collaborator

Oh, I can see your confusion, we don't have a proper documentation for this usecase!
To fix your tests you need to use an array form of they keyPath argument (first one):

  expect(response).toHaveProperty(['TLS1.0'])
  expect(response).toHaveProperty(['TLS1.1'])
  expect(response).toHaveProperty(['TLS1.2'])
  expect(response).toHaveProperty(['X.509Token'])

@thymikee thymikee changed the title expect().toHaveProperty fails with a dot (".") in the property name. Document how expect().toHaveProperty works with a dot in the property name. Feb 24, 2018
@thymikee
Copy link
Collaborator

The changes I'd like to see in the docs are following:

const houseForSale = {
  bath: true,
  bedrooms: 4,
  kitchen: {
    amenities: ['oven', 'stove', 'washer'],
    area: 20,
    wallColor: 'white',
+   'nice.oven': true,
  },
};
  expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven');
+ expect(houseForSale).toHaveProperty(['kitchen', 'nice.oven'])
  expect(houseForSale).not.toHaveProperty(['kitchen', 'open']);

to be found here: https://github.com/facebook/jest/blob/master/docs/ExpectAPI.md#tohavepropertykeypath-value

@hawkeye64
Copy link
Author

Thank you so much! That worked perfectly. Faith restored. ;)

@hawkeye64 hawkeye64 reopened this Feb 24, 2018
@hawkeye64
Copy link
Author

Oops! I see you changed the title, so I won't close this as it should be added to the documentation. Thanks!

@czystyl
Copy link
Contributor

czystyl commented Feb 26, 2018

I can update documentation 👍

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants