jest-partial icon indicating copy to clipboard operation
jest-partial copied to clipboard

Partial Arrays Order

Open gdelmas opened this issue 4 years ago • 3 comments

hi @smeijer,

thanks for creating this matcher. i think there is quite some need for it.

i believe how it works with partial objects is obvious. the word partial and arrays opens up some questions though.

i would have assumed that a partial array needs to be a slice of a whole array. meaning that the consecutive order of the elements in the partial have to be the same. i can see how the wording of the underlying matcher used, arrayContaining is more obvious.

in my case, checking the order of arrays in the partial match is important. do you think that is something that might be in scope for this matcher?

gdelmas avatar Sep 29 '20 17:09 gdelmas

Hi @gdelmas,

I believe that might be a nice addition. We can add options as second argument, or even a second matcher if that's more convenient.

I'm happy to support this, as long as the default behavior stays the same.

Are you able/willing to submit a PR?

smeijer avatar Sep 29 '20 17:09 smeijer

i was just experimenting around. in my specific case, i was needing partial objects, and nested arrays of partial objects. but not partial arrays. i think the following use cases exist:

  1. array items and objects all have to match (partially on their own)
  2. an array slice has to match partially (in the same way as above)

i think 1. would be as simple as changing https://github.com/smeijer/jest-partial/blob/master/src/index.js#L3 to

return obj.map((x) => partial(x));

i think the options object is good, to allow for reuse of the matcher. in this case there could be an option partialArrays: false. this could be used by another matcher .toMatchPartialObject() or .toDeepMatchPartialObject()?

option 2, would need to add that slice detection. i see how up until now everything was quite simply using jest matchers under the hood. do you know if there is a matcher to match a slice of an array? otherwise i would naively assume this can be done by iterating over the array and checking if items partial match consecutively. for sure it will be a little more involved than 1.

i think i could provide an MR for the 1. soon. for 2. maybe we can see later if need arises? what do you think about the plan and the api?

gdelmas avatar Sep 29 '20 18:09 gdelmas

Do you have an example test that would currently break, and that you expect to pass?

but not partial arrays

That sounds like the native expect(...).toMatchObject(...) matcher.

Regarding the API, I think .toMatchPartialObject or .toDeepMatchPartialObject sound confusting. Just by the name, I have no idea how it differs from toMatchPartial :sweat_smile:

smeijer avatar Sep 29 '20 19:09 smeijer