-
Generate Pact Contract in Cypress Ensure your Cypress test suite is generating a Pact contract. This can be done by making HTTP requests using Pact's pact library in your Cypress tests.
-
Install Pact CLI (if not already installed) To publish the generated contract, you’ll need the Pact CLI. You can install it globally with npm or yarn: npm install -g @pact-foundation/pact-cli
-
Publish the Pact Contract After Cypress generates the contract file, use the Pact CLI to publish it to your Pact Broker. Run this command in your CI/CD pipeline or as a post-test script in your Cypress configuration.
The command structure looks like this: pact-broker publish <pact_file_directory> --consumer-app-version --broker-base-url <broker_url> --broker-token Replace the placeholders with:
<pact_file_directory>: Path where the Pact file is saved by Cypress. : The version of your consumer application (could be a commit hash or version number). <broker_url>: The URL of your Pact Broker. : Authentication token if your Pact Broker requires it. Example:
pact-broker publish ./cypress/pacts --consumer-app-version $(git rev-parse --short HEAD) --broker-base-url http://localhost:9292 --broker-token $PACT_BROKER_TOKEN
-
Automate in CI/CD Pipeline Add the publishing command to your CI/CD pipeline (e.g., in a script section of GitHub Actions, CircleCI, or Jenkins) so that the contract is published after every build or test run.
-
Verify the Contract Publishing Once published, check your Pact Broker dashboard to confirm the contract is available and accessible by your provider services for verification.
This approach integrates Pact contract publishing within Cypress, ensuring every test run can update the contract in the Pact Broker automatically.