Ensure Best Practice Pact Tests with AI Code Review
We’re excited to introduce a new addition to the PactFlow AI toolkit: Code Review—now available in beta!
Designed for teams already using Pact, this feature helps ensure your contract tests follow best practices. Building on our existing AI-powered test generation and templating features, AI Code Review automatically inspects your Pact tests and offers actionable suggestions to improve quality, coverage, and maintainability.
Getting Started
Make sure you're on the latest version of the PactFlow CLI. Run:
pactflow-ai review
--test ./src/api.pact.spec.ts
--code ./src/api.js
--code ./src/product.js
Given the following input:
describe("GET /product/:id", () => {
test("returns 200 with a product", async () => {
await pact
.addInteraction()
.uponReceiving("a request for a product by id")
.withRequest("GET", "/product/1", (builder) => {
builder.headers({
Accept: "application/json",
Authorization: like("Bearer some-token"),
});
})
.willRespondWith(200, (builder) => {
builder.jsonBody(
like({
id: "1",
name: "Product 1",
type: "Type A",
})
);
})
.executeTest(async (mockserver) => {
// 🚨 this is bad practice, it should use the real API Client!
const product = axios.get(`${mockserver.url}/product/1`)
expect(product).toEqual({
id: "1",
name: "Product 1",
type: "Type A",
});
});
});
});
Here's a sample of what Code Review might return:
🔍 Review Completed - 2 recommendations found:
Recommendation 1:
💡 Suggestion: Refactor the test to invoke the real consumer code. By calling axios.get(...) directly, you're testing the mock server response, not the contract between your consumer code and the provider. Use the API class to ensure the test reflects actual consumer behavior.
🧩 Diff:
--- a/src/test/pact.test.ts 2025-01-01 10:00:00
+++ b/src/test/pact.test.ts 2025-01-01 10:00:00
@@ -35,7 +35,9 @@
.executeTest(async (mockserver) => {
- const product = axios.get(`${mockserver.url}/product/1`)
+ const api = new API(mockserver.url);
+ const product = await api.getProduct("1");
expect(product).toEqual({
id: "1",
name: "Product 1",
For details on how to review tests, and explore additional AI features, check out our documentation.
Available Now
AI Code Review is available for JavaScript/TypeScript and Java users and included in all cloud plans—no upgrade required. Support for our remaining languages is on the way.