Hi! I am pretty new to Pact and I am trying to mat...
# pact-js
m
Hi! I am pretty new to Pact and I am trying to match the dynamic Map of arrays example is like: { // rendered property is a problem rendered: { ‘2023-01-01’: [0,1] } } each key in rendered should be a date and it should contain array of numbers. I can’t figure it out
can anyone help with that? I am on the latest version of pact-js. Need to describe matching for structure like this:
Copy code
{
   rendered: {
        '2023-01-01': [0,1],
        '2023-05-23': [2],
        '2023-06-01: [0]
} 
}
In summary rendered is a map, where keys are dates string and values are arrays of number
m
I think the
eachValueMatches
rule is probably what you want. It lets the keys be dynamic, but you can specify the shape of the value. Here is an example test: https://github.com/pact-foundation/pact-js/blob/master/examples/v4/matchers/consumer.spec.ts#L73
m
Copy code
rendered: eachValueMatches({'2023-01-01': [0, 1]}, atLeastOneLike(1, 2))
but it breaks the test as instead of the example value, mockprovider returns “pact’s stuff” around it: what am I doing wrong?
m
Why are you asserting the pact simply did what you asked it? That's not a helpful test
You can use the reify function to strip that away though
m
I just follow the examples tbh and that’s exactly what was done there.
m
Remember: the point is to unit test your API client, not assert what the API returned (pact will do this for you)
m
I am more than happy to do it better.
m
Fair, we should make them clearer
m
I agree with testing API client! The issue is that the API client fails in this way as it cann’t get the rendered values really as it tries to get it from res.rendered, not res.rendered.values From what you saying it can be done in some way?
I am asserting api client response, not what mock returns, but the first one is a result of the second one
I’ve just tried with:
Copy code
.willRespondWith({
                    status: 200,
                    headers: {
                        'Content-Type': 'application/json; charset=utf-8'
                    },
                    body: reify(expectedPDDData)
                });
but it didn’t strip that guff
m
No, reify strips the matchers away. Pact needs the matchers to know how to...match
Add the reify to your assertion around the
expected
m
We’ve tried sth like this:
Copy code
rendered: [
    eachKeyMatches({ '2023-01-01': eachLike(0) }),
    eachValueMatches({ '2023-01-01': eachLike(0) }, like(eachLike(0))),
]
Copy code
$.rendered -> Type mismatch: Expected List [{"2023-01-01":[0]},{"2023-01-01":[0]}] but received Map {"2023-01-01":[0]}
Consumer test went through! Thanks, but the match does not work as we want 😞 We want to allow 0..n keys in that object (rendered). I will highly appreciate suggestion how to fix this
🙏
m
Copy code
rendered: [
    eachKeyMatches({ '2023-01-01': eachLike(0) }),
    eachValueMatches({ '2023-01-01': eachLike(0) }, like(eachLike(0))),
]
^^ is rendered an array though? I think you want:
Copy code
rendered: eachValueMatches({ '2023-01-01': [0] }, eachLike(0)),