Hi we had contract tests implemented for one of ou...
# pact-js
v
Hi we had contract tests implemented for one of our services. but because of some issues we had to downgrade from pact 10.* to pact 9.* As a part of this downgrade, we had to switch our test to using pact v2 instead of v3 - but as result, our tests on consumer side started to fail randomly. Please find attached full test class. Maybe you have any ideas why it happens? Thank you in advance!
m
There are multiple places you aren't handling promises correct. E.g. line 300, 328 and the entire afterAll stanza
Anytime I hear "random failures" in JS I hear "probably mishandled promise"
What issues in v3 are you facing by the way that requires downgrading?
t
Maybe you copied it in for convenience of posting here, but this test code includes a full implementation of the client - you're meant to test your actual client code, not make the request separately from your client code
Almost every method of
provider
is a promise, so you need to await or return them all. Also, you don't need to call
writePact
. Calling
finalize
does this for you.
Also, you're giving an argument to
provider.verify()
, but it didn't take any in V9. You should first await your request, and then await
provider.verify()
. Normally you would do the
verify
in an
afterEach()
If you need examples of how to use v9, I would look in https://github.com/pact-foundation/pact-js/tree/v9.18.1/examples
The APIs are not compatible between v9 and v10, you'll need to do a fairly significant rewrite.
Another issue is that it looks like you're misusing the
given
- this is for provider states, not for describing the name of the request.
v
@Timothy Jones I've refactored the test according to the example from your link, but now all tests fail with 2 errors:
getaddrinfo ENOTFOUND undefined
and
Pact verification failed - expected interactions did not match actual.
t
That doesn’t match the examples. Please read the documentation
m
Copy code
headers: '{"Accept", "application/json"}',
I’m just on mobile, but had a quick peek. The value should be a key/value object, not a string
and in
Copy code
afterEach(() => provider.verify);

        afterAll(() => provider.finalize);
you’re not calling the methods, so I’m surprised if that works
v
@Matt (pactflow.io / pact-js / pact-go) I've removed matching headers and calls of verify/finalize (since they are not used in https://raw.githubusercontent.com/pact-foundation/pact-js/v9.18.1/examples/jest/__tests__/index.spec.js) but still getting
Pact verification failed - expected interactions did not match actual