No you can't. What's the use case?
# pact-js
m
No you can't. What's the use case?
a
i know it’s dangerous.. 😕 i’m testing some post/get api. Now, in the POST i send. a certain payload with several unique identifiers… so if the DB is not cleared the test will fail the 2nd time it is run because it’ll clash So one option was above, change one of the uniques.. another option is clear DB (not ideal) and yet another option would be to mock the DB calls…
m
Yes, I believe they should work on JSON bodies. We may even have an example in the GH repo
Actually what you're asking for isn't very dangerous at all 🤣
a
slightly dangerous, since who knows what i’m changing the payload on the provider side 😱
t
Matt, does
fromProviderState
do any matching? It probably should.
If not, I agree, it would let you have non-contract contract tests 😕
a
my work around: on the provider side, under
stateHandle
for the create test, i first delete said entity
m
that’s an approach too
fromProviderState
can definitely add properties to any place a matcher can be used. See https://github.com/pact-foundation/pact-js/blob/6a38be8610d41fb2e786bbad4b7edf1a8c[…]v3/provider-state-injected/consumer/transaction-service.test.js for an example in a body
(but clearing out state between tests is the recommended way of doing this!)
the other function is really a workaround for times when there is random IDs generated on the provider side that can’t easily be stubbed/mocked out
Matt, does
fromProviderState
do any matching? It probably should.
I think it does as it is a Matcher and a generator
🙌 1
Yep, it does. Modified the provider example above to return a string as an ID instead of a number:
Copy code
1) Verifying a pact between TransactionService and AccountService Given Account Test001 exists - a request to get the account details
    1.1) has a matching body
           $.id -> Expected '27' to be an integer value
a
ahhh… so i can use the
fromProviderState
inside my body/payload object… interesting… that way i can make sure to change just what i need and still use the payload from the pact
m
Yes. But I’d still advocate clearing state, personally.
👍 3