I am flattening (using flat) deeply nested objects and performing business logic on it based on the flattened keys/values. In example:
const flattenedObject = flatten({ key: 'value', nested: { key: 'value' } });
console.log(flattenedObject); // { key: 'value', 'nested.key': 'value' }
I would like to test that a key is on my object and has a particular value after being flattened, however because .toHaveProperty uses dot notation for checking deeply nested references I am unable to do so.
Example:
expect(flattenedObject).toHaveProperty('nested.key', 'value');
Fails with:
expect(object).toHaveProperty(path, value)
Expected the object:
{"key": "value", "nested.key": "value"}
To have a nested property:
"nested.key"
With a value of:
"value"
The work around for this is:
expect(flattenedObject['nested.key']).not.toBeUndefined();
expect(flattenedObject['nested.key']).toEqual('value');
Example/testable repository: https://github.com/dmmulroy/jest-toHaveProperty-feature-request