Hi! :wave: I am experiencing an issue with `fromP...
# pact-js
s
Hi! 👋 I am experiencing an issue with
fromProviderState
function. Relevant issue from a year ago, which seems like it has been abandoned https://github.com/pact-foundation/pact-js/issues/1088 When I use
fromProviderState
with request path, the mock server matches any request. •
fromProviderState('/users/${userId}', '/users/1234')
◦ This should mean that provider-side uses
userId
to construct
/users/<real-user-id>
and consumer side should match
/users/1234
Expected behaviour:When I make request to
/users/random-stuff
Then test should fail, as the server should expect
/users/1234
Actual behaviour:When I make request to
/users/random-stuff
Then test succeeds and server gives a response 🤯 I also tried to use
equal
(equality matcher), as according to the docs, "`equal` - Value that must be equal to the example. This is mainly used to reset the matching rules which cascade." This seemed like a perfect candidate for this matcher, as we are matching inside the
fromProviderState
🤷 I assumed that maybe it checked that
/users/1234
and
/users/random-stuff
are both strings. • I tried:
fromProviderState('/users/${userId}', equal('/users/1234'))
, but this still allowed
/users/random
to be used... ⚠️ IMO this issue is quite pressing, because of two things: • Getting path variables from provider state is super common use case, so using
fromProviderState
is necessary • Consumer side should not allow any data, when
fromProviderState
is used, as it allows breaking the consumer side without breaking the tests. Simplified example: ◦ Initial JS API: `const fetchUsers(id) => fetch(
/users/${id}
)` ◦ Test:
fromProviderState('/users/${userId}', '/users/1234')
◦ Dev changes JS API: `const fetchUsers(id) => fetch(
/users/changes/${id}
)`, breaking the contract ◦ Test still passes Open to all help and suggestions 🙇. Maybe there is a workaround to achieve the same results or it's already fixed somewhere. Thanks!
👋 1
This is also relevant with using auth key from provider state. Consider the following: Initial JS API:
Copy code
...
   headers: {Authorization: `Bearer ${token}`}
...
Test:
Copy code
...
  Authorization: fromProviderState(`Bearer ${token}`, 'Bearer aa-bb-cc')
...
Dev changes JS API:
Copy code
...
   headers: {Authorization: `Bearer-no-more ${token}`}
...
Test still passes...
m
Thanks for the report, and whilst the issue has slipped down it’s still there.
Getting path variables from provider state is super common use case, so using
fromProviderState
is necessary
I would argue it’s not that common, at least, there are other ways to achieve this use case. But that’s not really the point, the software should work whether or not the feature is commonly used or not.
👍 1
I believe the issue is upstream - linked here: https://github.com/pact-foundation/pact-reference/issues/447
s
Glad to hear! Thanks! 🙇
Might have over exaggerated the "super common use case" here, but yeah, still used, and a valid approach. Actually in the meantime, for the
path
, I used an UUID in consumer: • `path: `/users/${userId}`` ... and then passing the
userId
in the given block. •
.given('user with id exists', {userId})
... this ensures the same data in consumer and provider. Just the source is consumer, not provider. But the case for auth is still very valid, as we want to use a real token on the provider side and a mock one on the consumer side.
m
It looked like a simple fix, but turns out there might be another underlying problem that was hidden by the fact this generator was also a matcher. Follow along here: https://github.com/pact-foundation/pact-js/pull/1223
👍 1