question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: .toHaveProperties(arguments: string[]) matcher

See original GitHub issue

What 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mattphillipscommented, Feb 24, 2018

@elad-yosifon have you checked out jest-extended? It has support for toContainKeys which is the same API to your example toHaveKeys.

@WederPachecoSilva jest-extended also has APIs for checking entries (key/value pairs) in an object see toContainEntries

1reaction
elad-yosifoncommented, Feb 24, 2018

@SimenB @WederPachecoSilva … maybe changing the function to

expect().toHaveKeys(...keys :string[])

will be a better idea?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found