Jest: Document how expect().toHaveProperty works with a dot in the property name.

Created on 24 Feb 2018  路  5Comments  路  Source: facebook/jest

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"
}

Documentation good first issue

Most helpful comment

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

All 5 comments

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'])

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

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

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

I can update documentation 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samzhang111 picture samzhang111  路  3Comments

ticky picture ticky  路  3Comments

gustavjf picture gustavjf  路  3Comments

kentor picture kentor  路  3Comments

stephenlautier picture stephenlautier  路  3Comments