I have 2 questions: 1 - I saw that there isn’t an ...
# pact-js
y
I have 2 questions: 1 - I saw that there isn’t an option to verify an empty array, but I think there are some cases that it should be checked, for example: Assume you have the following get request: getFieldsByCountry the response is
[{ l1: fields[{k1, v1}] ,
l2: fields[]}
In other words, what about the case of API that has children that has attrbiute as value with array type, but some of the array could be an empty array, I think this is a common case saw this post as well https://stackoverflow.com/questions/61772433/how-to-verify-pact-when-data-returns-items-with-or-without-children-filled-in-o and I understood the issue if min value would be 0, but from the other case, i think it is a common case that couldn’t be checked 2 - How can I verify a request with a dynamic id (url param). I looked into the documentation, and didn’t see a way to do that. For example, I have an api that generate some request and the response is the id, then I want to call to a get request, by the id (the id is a url param) (the id is created dynamically)
t
there is absolutely an option to verify an empty array
☝️ 1
you just specify an empty array
there isn't an option to say "like this type, but minimum none", because that means that empty arrays that never return the right type might inappropriately pass. For full verification, you need to test both the empty and the non-empty array cases
question 2 - stackoverflow seems to be down (!) so I can't answer that now, sorry 😞
Ah, wait - yes, you can do that with provider states. Have a look at
fromProviderState
Remember that in a pact test, each interaction is independent - you use provider state to set any preconditions.
☝️ 1
y
@Timothy Jones thanks I will try this.
@Arnon Ram pls take a look on the “fromProviderState”
@Timothy Jones when I tried to set min to none, I got ERROR pact@9.18.1: error making http request: Request failed with status code 500 Also in the docamantion I saw that the min is 1. maybe this is only in the new version
t
For the reasons I mentioned above, you can't set the min as none. You just use an empty array literal. No matcher is necessary
d
I am not sure I understand why you cannot have an Either matcher saying its empty or it must contain something with a shape that has been defined
t
because those are different cases
If we say the pact is:
Copy code
{
   optionalArray: either([], { /* some object */ })
}
then
{ optionalArray: [] }
passes that test every single time
Pact is not about describing what is possible
it's about testing that your provider and your consumer can actually talk to each other
so, the correct practice is to have two interactions
Copy code
state: "no arrays"
GET /arrays 
responds with { optionalArray: [] }
and
Copy code
state: "some arrays"
GET /arrays 
responds with { optionalArray: eachLike( { /* some object */ } ) }
"Ah" you say - "but my consumer doesn't actually care about the contents of that array" Right, then it doesn't go in the pact test. Pact will ignore contents that isn't in the pact.
chefkiss 1
You only test what you are expecting to be there
and if you're allowed to have optionals, you don't necessarily test that it is going to be there
m
Pact is a tool that implements spec'fication by example. It’s not that schemas are bad, it’s that they are just abstract, and with Pact we are trying to reduce ambiguity. As Tim points out, we’re not trying to build a schema of what’s possible (that’s the provider’s job). We’re trying to get out all of the uses cases of the consumer as clearly as we can. We are also unit testing the consumer, and by doing so providing evidence that the consumer actually does need the data and can deal with it, and build out the surface a’ea of the provider that's actually used. Logical conditions tend to increase ambiguity rather than reduce it.
(I’m not sure why that link is funny, but every time I pasted it in, I couldn’t type in the box without all sorts of weird things happen - so know that I desperately want to remove the
'
but can’t 😆 . I think I might be in a late digital-era Saw movie……)