question about matching arrays. i have a pact th...
# general
h
question about matching arrays. i have a pact that i'd also like to reuse as a stub for testing the consumer ui. my goal is to generate a realistic response that i can reuse as a stub for a ui integration test and also a minimalistic pact definition for the provider to verify, eg atLeastOneLike. is there a matching option that would generate something to support this? pseudo specification
Copy code
{
    picklists: atLeastOneLike([
      {id: 1, name: 'She/Her'},
      {id: 2, name: 'He/Him'},
      {id: 3, name: 'They/Them'},
      {id: 4, name: 'Other'},
    ])
  }
so that the generated stub response for the consumer definition is:
Copy code
{
    picklists: [
      {id: 1, name: 'She/Her'},
      {id: 2, name: 'He/Him'},
      {id: 3, name: 'They/Them'},
      {id: 4, name: 'Other'},
    ]
  }
and the generated matcher for the provider is:
Copy code
"matchingRules": {
    "body": {
      "$": {
        "combine": "AND",
        "matchers": [
          {
            "match": "type"
          }
        ]
      },
      "$.picklists": {
        "combine": "AND",
        "matchers": [
          {
            "match": "type",
            "min": 1
          }
        ]
      }
    },
t
Pact doesn’t support “or” on purpose- see the FAQ
m
I think Hazem is asking about the pact to store all different examples in it that would still comply with the matching rules. The way it works now is you provide a single example e.g.
Copy code
{
    picklists: eachLike({id: 1, name: 'She/Her'}, 4)
  }
and then if you used it as a stub, you’d get back
Copy code
[
   {id: 1, name: 'She/Her'},
   {id: 1, name: 'She/Her'},
   {id: 1, name: 'She/Her'},
   {id: 1, name: 'She/Her'}
]
👍🏽 1
t
Right. I think it would be good to extend to:
Copy code
eachLike(itemDescriptionWithMatchers, exampleArray?)
So that you could say:
Copy code
eachLike(
  {id: number(), name: string()},  
  [
      {id: 1, name: 'She/Her'},
      {id: 2, name: 'He/Him'},
      {id: 3, name: 'They/Them'},
      {id: 4, name: 'Other'},
  ]
)
👍🏽 1
Although, if this:
i have a pact that i'd also like to reuse as a stub for testing the consumer ui.
is the use case, I think that sounds unnecessarily complex. I think it would be better to stub the business object that you assert on in your pact test. For example, if your pact test looks something like:
Copy code
pact.addInteraction(/* whatever */)

it("returns the correct object", () => {
   const businessObject = myApi.getWhatever()
   expect(businessObject).toEqual(expectedBusinessObject)
})
Then you can use
expectedBusinessObject
in stubs, stubbing out
myApi
completely 🙌
m
Yep, that would be nice. I think the use case is not for the direct pact unit test, but re-using the generated pact file from the stub server later on in UI tests (e.g. cypress). It’s nice to use the pact file for those, because now you’re certain the APIs you’re faking in your UI tests are valid scenarios the provider supports.
Pact has its limitations for those scenarios, but if you can get by with them it’s a real boon
t
Yes, we are on the same page about the use case. I think a mock (that you assert using pact) is a better fit than a stub api in that case
It’s the same thing we do in a provider test
h
yes - your earlier discussion above is what i was looking for, eg
Copy code
eachLike(itemDescriptionWithMatchers, exampleArray?)
apologies, i may have used stub a little too loosely here. 😁 i was referring to the feature in
pactfoundation/pact-stub-server
where we can start a local stub-server running our pact definitions. so in this case, i was hoping to define a pact basically as suggested in the eachLike example.
t
Yes, that's my understanding of what you're wanting to do with the stub server. My recommendation is to use the test fixtures from your contract test (rather than the contract) to do the stubbing, as it will be simpler, with less moving parts but similar strengths and weaknesses. But, the approach of the stub server is fine too
h
i see your point, however in our case we want to specifically use a stub server to host the api responses as this level of integration testing seems to meet the needs/desires from two separate front-end component teams. is the request above easily doable?
Copy code
eachLike(itemDescriptionWithMatchers, exampleArray?)
we're using pact-js and this would be a great enhancement for our usage there.
t
No, I don't believe this is currently supported. @uglyog - can you confirm? You can report this as a feature request against pact-js in this repo: https://github.com/pact-foundation/pact-js
h
thanks for the feedback, i went ahead and logged the feature request since i've already dug around in the code.
👍 1