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

Enhance toHaveProperty() to allow array selection #7284

Closed
stephen-last opened this issue Oct 26, 2018 · 4 comments
Closed

Enhance toHaveProperty() to allow array selection #7284

stephen-last opened this issue Oct 26, 2018 · 4 comments

Comments

@stephen-last
Copy link

🚀 Feature Proposal

Enhance the .toHaveProperty() matcher to allow array selection, similar to lodash/get.

Motivation

Will allow very easy assertions on very complex objects which contain nested arrays/objects.

Example

const stateData = {
  attributes: {
    all: [
      {
        name: `Colour`,
        values: [
          { name: `Black`, products: [2, 3] },
          { name: `Orange` },
          { name: `Green` },
          { name: `Purple` }
        ]
      }
    ]
  }
}

expect(stateData).toHaveProperty(`attributes.all[0].values[0].products`, [2, 3])
expect(stateData).toHaveProperty(`attributes.all[0].values[2].name`, `Green`)

Pitch

It's an enhancement to an existing matcher which would allow smaller and cleaner tests when asserting against large and complex objects which contain arrays.

As an enhancement, it wouldn't break any existing functionality.

Would bring the path spec in line with the very popular lodash library.

Would meet many peoples expectations of Jest, as I assumed this is a common task and would already be available.

Notes

If I've missed something and asserting deep properties in objects containing arrays is already easy, then sorry, I'm happy to be pointed in the right direction.

@thymikee
Copy link
Collaborator

Right now you can do:

expect(stateData).toHaveProperty(
  ['attributes', 'all', 0, 'values', 0, 'products'],
  [2, 3],
);

It's obviously not the same, but possible. lodash/get syntax is nice, but privately I'm very against this library as it looses all type information (at least in Flow codebases) and I prefer to use idx.

I'm open to discussion on whether we want this API or now :)

@stephen-last
Copy link
Author

@thymikee Thank you for that, I didn't know you could access arrays like that, I was expecting to have to use an ugly set of nested expect.objectContaining() and expect.arrayContaining().

Just checked the docs again and there is indeed an example already, which I missed. Sorry, my mistake.

Interestingly, I've been trying to get started with Flow this week, and ran into the issues you reference with lodash right-away.

FWIW, I'm more than happy with the array path method for toHaveProperty().

@thymikee
Copy link
Collaborator

Closing then (we have too many issues 😫), but happy to accept a PR implementing that, if anybody is interested :)

@rdsedmundo
Copy link

+1 for this, honestly I don't really care about types in this situation of doing a expect. And the existent dot notation already erases all of that, so adding the array on top won't make any difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants