Feature Request: .toHaveProperties(arguments: string[]) matcher
See original GitHub issueWhat is the current behavior? Currently Jest offers .toHaveProperty to check for a specific property inside an object but I couldn’t see an easy way of checking for multiples properties. I am aware that Jest offers expect.extend() to make possible for us to customize a matcher, but checking for more than one property seems quite usual to me. Wouldn’t it be interesting to have a .toHaveProperties(args) matcher that receives an array of strings to check?
A fast and naive prototype of what I am proposing using expect.extend:
const extend = {
toHaveProperties(received, args) {
const receivedProperties = Object.getOwnPropertyNames(received);
const pass = !args.some(val => receivedProperties.indexOf(val) === -1);
if (pass) {
return {
message: () => `expected ${received} not to have properties of ${args}`,
pass: true,
};
} else {
return {
message: () => `expected ${received} to have properties of ${args}`,
pass: false,
};
}
},
};
expect.extend(extend)
``
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Expect - Jest
The argument to expect should be the value that your code produces, and any argument to the matcher should be the correct value....
Read more >Expect · Jest
stringMatching (regexp) matches any received string that matches the expected regexp. You can use it instead of a literal value: in toEqual or...
Read more >Jasmine test for object properties - Stack Overflow
7 Answers 7 ; 4. I'm not seeing the toHaveProperty() method. Is this for jasmine? – BizzyBob ; 1. That method is not...
Read more >Jest partial matching on objects, arrays and functions
This test will pass as long as the array contains the string 'important' . Match an array in any order (unordered array). The...
Read more >Taking Advantage of Jest Matchers (Part 2) - Herding Lions
They're matchers that check to see if an array contains an item or if a string contains a substring. toContain and toContainEqual. toContain...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@elad-yosifon have you checked out jest-extended? It has support for
toContainKeys
which is the same API to your exampletoHaveKeys
.@WederPachecoSilva jest-extended also has APIs for checking entries (key/value pairs) in an object see
toContainEntries
@SimenB @WederPachecoSilva … maybe changing the function to
will be a better idea?