JestPact
Jest Adaptor to help write Pact files with ease
Features
Installation
npm i jest-pact --save-dev
OR
yarn add jest-pact --dev
Usage
pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
}
with supertest
pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, (provider, client) => {
}
Configuration
pactWith({PactOptions}, provider => {
}
export interface PactOptions {
provider: string;
consumer: string;
port?: number;
logLevel?: LogLevel;
}
export declare type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
Output
- Log files are written to /pact/logs
- Pact files are written to /pact/pacts
Example
You can use this with any http agent for sending your requests.
pactWith(
{ consumer: "MyConsumer", provider: "pactWith", port: pactPort },
async (provider: any) => {
test("should accept a valid get request to get a pet 1", async () => {
const postValidRequest: InteractionObject = {
state: "A pet 1845563262948980200 exists",
uponReceiving: "A get request to get a pet",
willRespondWith: {
status: 200
},
withRequest: {
method: "GET",
path: "/v2/pet/1845563262948980200",
headers: { api_key: "[]" }
}
};
await provider.addInteraction(postValidRequest);
const client = getClient();
await client
.get("/v2/pet/1845563262948980200")
.set("api_key", "[]")
.expect(200);
await provider.verify();
});
}
);
Example with SuperTest
You can use superagent as your http agent, it has a great assertion engine and as we instantiate the pact mock and http agent at the same time, we can assign random ports and take advantage of jests parallel execution.
pactWithSuperTest(
{ consumer: "MyConsumer", provider: "pactWithSuperTest" },
async (provider: any, client: any) => {
test("should accept a valid get request to get a pet", async () => {
const postValidRequest: InteractionObject = {
state: "A pet 1 exists",
uponReceiving: "A get request to get a pet",
willRespondWith: {
status: 200
},
withRequest: {
method: "GET",
path: "/v2/pet/1",
headers: { api_key: "[]" }
}
};
await provider.addInteraction(postValidRequest);
await client
.get("/v2/pet/1")
.set("api_key", "[]")
.expect(200);
await provider.verify();
});
}
);