Syam K
04/29/2023, 8:31 AMit("returns the requested employee", async () => {
provider
.given("I have an empoyee")
.uponReceiving("a request for getting the employee by name")
.withRequest({
method: "GET",
path: "/api/employees/name/Charles",
headers: { Accept: "application/json" },
})
.willRespondWith({
status: 200,
headers: { "content-type": "application/json" },
body: GET_EMPLOYEE_BY_NAME_EXPECTED_BODY,
});
return provider.executeTest(async (mockserver) => {
const employeeService = new EmployeeService(mockserver.url);
const response = await employeeService.getEmployeeByName("John");
expect(await response.headers["content-type"]).toEqual(
"application/json"
);
expect(await response.data).toEqual(GET_EMPLOYEE_BY_NAME_EXPECTED_BODY);
expect(await response.status).toEqual(200);
});
});Timothy Jones
04/29/2023, 10:39 AMTimothy Jones
04/29/2023, 10:41 AMSyam K
04/29/2023, 10:42 AMimport { MatchersV3 } from "@pact-foundation/pact";
import EmployeeService from "../../../src/apis/EmployeeService.js";
describe("Employee Clients Service", () => {
const GET_EMPLOYEES_EXPECTED_BODY = [
{
id: 1,
name: "Amy",
surname: "Jakline",
role: "Software Engineer",
},
{
id: 2,
name: "Jake",
surname: "Folker",
role: "Test Analyst",
},
{
id: 3,
name: "Charles",
surname: "Hucker",
role: "Scrum Master",
},
{
id: 4,
name: "Terry",
surname: "Roland",
role: "Developer",
},
{
id: 5,
name: "Rosa",
surname: "Marie",
role: "Technical Solution Architect",
},
];
//afterEach(() => provider.verify());
const GET_EMPLOYEE_BY_NAME_EXPECTED_BODY = [
{ id: 3, name: "Charles", surname: "Hucker", role: "Scrum Master" },
];
describe("GET All Employees", () => {
it("returns all employees", async () => {
provider
.given("I have a list of empoyees")
.uponReceiving("a request for getting all employees")
.withRequest({
method: "GET",
path: "/api/employees",
headers: { Accept: "application/json" },
})
.willRespondWith({
status: 200,
headers: { "content-type": "application/json" },
body: GET_EMPLOYEES_EXPECTED_BODY,
});
return provider.executeTest(async (mockserver) => {
const employeeService = new EmployeeService(mockserver.url);
const response = await employeeService.getEmployees();
console.log("response1: " + response);
expect(await response.headers["content-type"]).toEqual(
"application/json"
);
expect(await response.data).toEqual(GET_EMPLOYEES_EXPECTED_BODY);
expect(await response.status).toEqual(200);
});
});
});
describe("GET Employee By Name", () => {
it("returns the requested employee", async () => {
provider
.given("I have an empoyee")
.uponReceiving("a request for getting the employee by name")
.withRequest({
method: "GET",
path: "/api/employees/name/Charles",
headers: { Accept: "application/json" },
})
.willRespondWith({
status: 200,
headers: { "content-type": "application/json" },
body: GET_EMPLOYEE_BY_NAME_EXPECTED_BODY,
});
return provider.executeTest(async (mockserver) => {
const employeeService = new EmployeeService(mockserver.url);
const response = await employeeService.getEmployeeByName("Charles");
console.log("response2: " + response);
expect(await response.headers["content-type"]).toEqual(
"application/json"
);
expect(await response.data).toEqual(GET_EMPLOYEE_BY_NAME_EXPECTED_BODY);
expect(await response.status).toEqual(200);
});
});
});
});Timothy Jones
04/29/2023, 10:43 AMTimothy Jones
04/29/2023, 10:44 AMSyam K
04/29/2023, 10:44 AMSyam K
04/29/2023, 10:45 AMimport * as path from 'path';
import { PactV3 } from "@pact-foundation/pact";
global.port = 8081
global.provider = new PactV3({
port: global.port,
log: path.resolve(process.cwd(), "__tests__/contract/logs", "mockserver-integration.log"),
dir: path.resolve(process.cwd(), "__tests__/contract/pacts"),
spec: 2,
logLevel: 'INFO',
pactfileWriteMode: "overwrite",
consumer: "Frontend",
provider: "EmployeeService",
})Timothy Jones
04/29/2023, 10:45 AMit) as async, but it won’t matter if you doSyam K
04/29/2023, 10:45 AMTimothy Jones
04/29/2023, 10:46 AMTimothy Jones
04/29/2023, 10:46 AMTimothy Jones
04/29/2023, 10:46 AMTimothy Jones
04/29/2023, 10:47 AMSyam K
04/29/2023, 10:47 AMTimothy Jones
04/29/2023, 10:49 AMMatt (pactflow.io / pact-js / pact-go)
I think it might be an improvement to have pact add a header saying the connection should be closed. But also, if it did that then I think this issue would be maskedthat’s a good point. Interesting. My gut feel is that it probably still makes sense for us to add that setting, as it feels more “infrastructure-y” than contract-testing.
Timothy Jones
05/01/2023, 10:15 PMMatt (pactflow.io / pact-js / pact-go)
It would be cool to have a “flaky” mode, with injected failures / problems for people using the mock server to back part of a black box test or something.I’m reminded of this post by Tal when at PageUp: https://medium.com/@rotbart/driving-api-client-resiliency-how-to-enforce-postels-law-by-violating-postel-s-law-4fd6ef9da205