example-hello-world
This is a very basic Pact example, demonstrating the simplest use of a Pact Consumer
Mock provider setup
To create a Pact, we first need to define a relationship between Consumer and Provider.
This gives a mock Provider, which our Consumer can interact with.
- js-v2
- js-jest-pact-v2
- python-v2
- js-v3
- js-jest-pact-v3
// Configure our Pact mock Provider
const mockProvider = new Pact({
consumer: "BearServiceClient",
provider: "BearService",
cors: true,
dir: "./output/pacts",
});
// Configure our Pact mock Provider
pactWith(
{
consumer: "BearServiceClient",
provider: "BearService",
dir: "./output/pacts",
},
(mockProvider) => {
# (1) Configure our Pact mock Provider
mock_provider = Consumer("BearServiceClient").has_pact_with(
Provider("BearService"),
host_name=PACT_MOCK_HOST,
port=PACT_MOCK_PORT,
pact_dir=PACT_DIR,
log_dir=LOG_DIR,
)
// Configure our Pact mock Provider
const mockProvider = new PactV3({
consumer: "BearServiceClient",
provider: "BearService",
cors: true,
dir: "./output/pacts",
});
// Configure our Pact mock Provider
pactWith(
{
consumer: "BearServiceClient",
provider: "BearService",
dir: "./output/pacts",
},
Pact creation
Each Pact is declared in a "given, when, then" style.
Following the "arrange, act, assert" pattern, we:
- arrange the expected interactions
- act on the consumer to perform the interaction
- assert that the mock provider received the specific interaction
- js-v2
- js-jest-pact-v2
- python-v2
- js-v3
- js-jest-pact-v3
// Arrange: declare our expected interactions
const expectedResponse = {
name: "Polar",
colour: "White",
};
await mockProvider.addInteraction({
state: "There are some bears",
uponReceiving: "A request for the Bear species with id 1",
willRespondWith: {
status: 200,
body: expectedResponse,
},
withRequest: {
method: "GET",
path: "/species/1",
},
});
// Act: make the Consumer interact with the mock Provider
const api = new BearConsumer(mockProvider.mockService.baseUrl);
const bear = await api.getSpecies(1);
// Assert: check the result is as expected
expect(bear).to.deep.equal(new BearSpecies("Polar", "White"));
describe("Test Bear species endpoint", () => {
const expectedResponse = {
name: "Polar",
colour: "White",
};
// Arrange: declare our expected interactions
beforeEach(() =>
mockProvider.addInteraction({
state: "There are some bears",
uponReceiving: "A request for the Bear species with id 1",
withRequest: {
method: "GET",
path: "/species/1",
},
willRespondWith: {
status: 200,
body: expectedResponse,
},
})
);
// Act: make the Consumer interact with the mock Provider
it("Returns a Bear species", () => {
return client.getSpecies(1).then((resp) => {
// Assert: check the result is as expected
expect(resp).toEqual(expectedResponse);
});
});
});
# Arrange: declare our expected interactions
(
mock_provider.given("There are some bears")
.upon_receiving("A request for the Bear species with id 1")
.with_request("GET", "/species/1")
.will_respond_with(200, body=expected)
)
with mock_provider:
# Act: make the Consumer interact with the mock Provider
species = consumer.get_species(1)
# Assert: check the result is as expected
# In this case the mock Provider will have returned a valid response
assert species.name == "Polar"
assert species.colour == "White"
# Make sure that all interactions defined occurred
mock_provider.verify()
// Arrange: declare our expected interactions
const expectedResponse = {
name: "Polar",
colour: "White",
};
mockProvider
.given("There are some bears")
.uponReceiving("A request for the Bear species with id 1")
.withRequest({
method: "GET",
path: "/species/1",
})
.willRespondWith({
status: 200,
body: { ...expectedResponse },
});
return await mockProvider.executeTest(async (mockserver) => {
// Act: make the Consumer interact with the mock Provider
const api = new BearConsumer(mockserver.url);
const bear = await api.getSpecies(1);
// Assert: check the result is as expected
expect(bear).to.deep.equal(expectedResponse);
});
const expectedResponse = {
name: "Polar",
colour: "White",
};
// Arrange: declare our expected interactions
beforeEach(() =>
provider
.given("There are some bears")
.uponReceiving("A request for the Bear species with id 1")
.withRequest({
method: "GET",
path: "/species/1",
})
.willRespondWith({
status: 200,
body: { ...expectedResponse },
})
);
// Act: make the Consumer interact with the mock Provider
execute("Returns a Bear species", (mockserver) =>
new BearConsumer(mockserver.url).getSpecies(1).then((resp) => {
// Assert: check the result is as expected
expect(resp).toEqual(expectedResponse);
})
);
Something else
Some other code snippets would go here when we have some
- None available
TODO: No code snippets available for this section
Pacts
- v2
- v2-v3 diff
- v3
{
"consumer": {
"name": "BearServiceClient"
},
"provider": {
"name": "BearService"
},
"interactions": [
{
"description": "A request for the Bear species with id 1",
"providerState": "There are some bears",
"request": {
"method": "GET",
"path": "/species/1"
},
"response": {
"status": 200,
"headers": {},
"body": {
"name": "Polar",
"colour": "White"
}
}
}
],
"metadata": {
"pactSpecification": {
"version": "2.0.0"
}
}
}
--- v2
+++ v3
@@ -8,7 +8,11 @@
"interactions": [
{
"description": "A request for the Bear species with id 1",
- "providerState": "There are some bears",
+ "providerStates": [
+ {
+ "name": "There are some bears"
+ }
+ ],
"request": {
"method": "GET",
"path": "/species/1"
@@ -25,7 +29,7 @@
],
"metadata": {
"pactSpecification": {
- "version": "2.0.0"
+ "version": "3.0.0"
}
}
}
{
"consumer": {
"name": "BearServiceClient"
},
"provider": {
"name": "BearService"
},
"interactions": [
{
"description": "A request for the Bear species with id 1",
"providerStates": [
{
"name": "There are some bears"
}
],
"request": {
"method": "GET",
"path": "/species/1"
},
"response": {
"status": 200,
"headers": {},
"body": {
"name": "Polar",
"colour": "White"
}
}
}
],
"metadata": {
"pactSpecification": {
"version": "3.0.0"
}
}
}