Skip to main content

Step 4 - Verify the provider

info

Move to step 4

git checkout step4

npm install

Learning Objectives

StepTitleConcept CoveredLearning objectivesFurther Reading
step 4Verify the consumer pact with the Provider APIProvider side pact test
  • Understand basic Provider-side Pact concepts
  • Place provider side testing in a broader testing context (e.g. where it fits on the pyramid)

We need to make the pact file (the contract) that was produced from the consumer test available to the Provider module. This will help us verify that the provider can meet the requirements as set out in the contract. For now, we'll hard code the path to where it is saved in the consumer test, in step 11 we investigate a better way of doing this.

Now let's make a start on writing Pact tests to validate the consumer contract:

In provider/product/product.pact.test.js:

const { Verifier } = require('@pact-foundation/pact');
const path = require('path');

// Setup provider server to verify
const app = require('express')();
app.use(require('./product.routes'));
const server = app.listen("8080");

describe("Pact Verification", () => {
it("validates the expectations of ProductService", () => {
const opts = {
logLevel: "INFO",
providerBaseUrl: "http://127.0.0.1:8080",
provider: "ProductService",
providerVersion: "1.0.0",
pactUrls: [
path.resolve(__dirname, '../../consumer/pacts/FrontendWebsite-ProductService.json')
]
};

return new Verifier(opts).verifyProvider().then(output => {
console.log(output);
}).finally(() => {
server.close();
});
})
});
To simplify running the tests, add this to `provider/package.json`:
// add it under scripts
"test:pact": "npx jest --testTimeout=30000 --testMatch \"**/*.pact.test.js\""

We now need to validate the pact generated by the consumer is valid, by executing it against the running service provider, which should fail:

❯ npm run test:pact --prefix provider

Verifying a pact between FrontendWebsite and ProductService

get product with ID 10
returns a response which
has status code 200 (FAILED)
includes headers
"Content-Type" with value "application/json; charset=utf-8" (FAILED)
has a matching body (FAILED)

get all products
returns a response which
has status code 200 (OK)
includes headers
"Content-Type" with value "application/json; charset=utf-8" (OK)
has a matching body (OK)


Failures:

1) Verifying a pact between FrontendWebsite and ProductService Given product with ID 10 exists - get product with ID 10
1.1) has a matching body
expected 'application/json;charset=utf-8' body but was 'text/html;charset=utf-8'
1.2) has status code 200
expected 200 but was 404
1.3) includes header 'Content-Type' with value 'application/json; charset=utf-8'
Expected header 'Content-Type' to have value 'application/json; charset=utf-8' but was 'text/html; charset=utf-8'

Pact Verification

The test has failed, as the expected path /products/{id} is returning 404. We incorrectly believed our provider was following a RESTful design, but the authors were too lazy to implement a better routing solution 🤷🏻‍♂️.

The correct endpoint which the consumer should call is /product/{id}.

Move on to step 5