Hi @channel, Need your expertise, please. I tried ...
# pact-js
s
Hi @channel, Need your expertise, please. I tried the Order API sample from https://docs.pact.io/5-minute-getting-started-guide. I am able to generate the pact file but the provider test is failing. The error is not that detailed I don’t know what I am missing. I am using Playwright as my Test Runner. I am attaching my code for both consumer and provider test and the pact file generated. Pls check if you have time. Thanks so much
y
Playwright is used for automation of web apps, rather than testing API's. Why have you chosen that as your test runner? A working code sample from that page is shown here https://github.com/YOU54F/pact-5-minute-getting-started-guide/tree/main Using a unit test runner, in this case mocha
s
I’ll try this. thank u thank u
y
Hey @slackk0t0, I got a bit of downtime this evening so had a quick look, got a working example from your sample files, thanks for providing those. https://github.com/YOU54F/pact-playwright-example the
main
branch has a working example as shown in the actions run here - https://github.com/YOU54F/pact-playwright-example/actions/runs/3325277851/jobs/5497793496 There does seem to be an issue showing failing verifications - https://github.com/YOU54F/pact-playwright-example/actions/runs/3325286761/jobs/5497811824 In the above test run, I changed the consumer to expect the provider to use the endpoint
/order
rather than
/orders
It correctly fails but the error messaging isn't useful - it might just be an issue with my test setup, I'll have a look into that tomorrow On the topic of Pact and the scope of your test, how does your client application that you are testing, interact with its provider? In your example repo, you are using Playwright to issue the requests to the provider, in which to generate the pact file. Traditionally you would be testing the actual client that issues the requests in your application, to ensure that what you encode in the pact file, is what your client application is issuing. Otherwise you tie you provider in knots trying to verify scenarios which don't match reality. Maybe providing some info on your use case would be useful 👍
s
You got it working. You are the best. I’ll check it out. Thanks so much. At this time, we don’t have a particular client. We are new to contract testing and trying out possible tools we can use in our team. We are eyeing Playwright to be our e2e automation tool and I thought I’d use it instead of axios/mocha for this Pact tests since in Playwright I can do the assertions and api requests using it. It’s all built in to Playwright. It doesn’t require too many third party tools.
y
Maybe take a look at https://github.com/pactflow/example-bi-directional-consumer-playwright for how you could use playwright to listen to routes being called from your web application under test, and mocking out those responses, using its native mocking. https://github.com/pactflow/example-bi-directional-consumer-playwright/blob/bb8e1fd5801fc4a47ad43bf36cbfc6a8b71a08ee/test/productByQuery.spec.js#L11-L24 we then transform that into a pact file, as we know that our client application issued that request. A more manageable scope itssplitting out your code that issues the api requests in your web application, so it is super quick to test. See below for a simple example https://github.com/TimothyJones/react-api-layer-example All the best in your Pact journey!
s
We don’t have a working web app yet. It’s really just me trying every tool I can find that’ll make our lives easier. Just creating some POC at the moment. But this one will surely help when we get to that point where we have a working web app. Thank you, Yousaf
Yey, it also worked on my machine. Thanks Yousaf!
I found that the provider tests are failing on gitbash terminal. They pass on powershell. That is soo odd. The error message does not really say anything about what’s missing.
Amy idea @Yousaf Nabi (pactflow.io) ?
m
I’ve just tried to take a look myself. It seems like something to do with Playwright, I wonder if it has something to do with it running (?) in a Chromium environment?
I can see that the core library is unable to print to stdout (which is where I’d expect to see it). It might be possible to resolve this by printing to file
(I can see that logs are being emitted from pact-core - the NodeJS library - but not the FFI library it uses. No logs/output are emitted from that process)
My guess is that because it’s in a Chromium environment, that output is supressed or redirected to a different file descriptor
I’d recommend not using Playwright as the test runner for Pact tests, and stick with something like Mocha/Jest for now.
s
ohhhh ok. i shall try mocha then. thank u
👍 1