User Tools

Site Tools


pact_contract_test

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
pact_contract_test [2026/01/05 07:07] – created pradnyapact_contract_test [2026/01/05 08:27] (current) – [Overview] pradnya
Line 3: Line 3:
 ===== Overview ===== ===== Overview =====
  
-  * +  * **BFF = Consumer** 
- +  * **Auth Service = Provider** 
-**BFF = Consumer** +  * Test verifies:
-  * +
- +
-**Auth Service = Provider** +
-  * +
- +
-Test verifies: +
       * request body       * request body
       * response structure       * response structure
       * role array       * role array
       * token fields       * token fields
 +
 +POST /auth/login - Request
 + <font 9px/inherit;;inherit;;inherit>json</font>
 +
 +<code>
 +{
 + "username": "john",
 + "password": "secret"
 +}
 +
 +</code>
 +
 +RESPONSE
 + <font 9px/inherit;;inherit;;inherit>json</font>
 +
 +<code>
 +{
 +  "accessToken": "jwt-string",
 +  "refreshToken": "jwt-refresh",
 +  "expiresIn": 900,
 +  "user": {
 +    "id": "u123",
 +    "email": "john@example.com",
 +    "displayName": "John Smith",
 +    "roles": ["ORDER.CREATE", "ORDER.VIEW"]
 +  }
 +}
 +
 +</code>
 +
 +Recommended Test Directory Structure
 +
 +<code>
 +/tests
 + ┣ unit
 + ┃ ┣ api
 + ┃ ┣ domain
 + ┃ ┗ infrastructure
 + ┣ integration
 + ┃ ┣ api
 + ┃ ┗ data
 + ┗ pact
 +   ┣ consumers
 +   ┃ ┗ auth
 +   ┃   ┗ login.pact.test.ts
 +   ┣ providers
 +   ┃ ┗ auth
 +   ┃   ┗ verify-login.pact.test.ts
 +   ┗ helpers
 +      ┗ pact-setup.ts
 +
 +</code>
 +
 +**Clean separation**
 +
 +^Folder^Purpose|
 +|''unit/'' |Pure logic tests|
 +|''integration/'' |DB / Redis / route tests|
 +|''pact/'' |Contract testing only|
 +|''pact/consumers/'' |Tests written in BFF repo|
 +|''pact/providers/'' |Optional — verification tests|
 +|''pact/helpers/'' |Shared setup utilities|
 +
 +Login Pact test lives in:
 +<code>
 +
 +/tests/pact/consumers/auth/login.pact.test.ts
 +
 +</code>
 +
 +**Helper Setup File**
 +
 +<code>
 +pact/helpers/pact-setup.ts
 +
 +</code>
 +
 +Create pact
 + <font 9px/inherit;;inherit;;inherit>ts</font>
 +
 +<code>
 +import path from "path";
 +import { PactV3 } from "@pact-foundation/pact";
 +
 +export function createPact(providerName: string) {
 +  return new PactV3({
 +    consumer: "BFF-Service",
 +    provider: providerName,
 +    dir: path.resolve(process.cwd(), "pacts"),
 +    logLevel: "INFO"
 +  });
 +}
 +
 +</code>
 +
 +**login.pact.test.ts** <font 9px/inherit;;inherit;;inherit>ts</font>
 +
 +<code>
 +import { createPact } from "../../helpers/pact-setup";
 +import { MatchersV3 } from "@pact-foundation/pact";
 +import axios from "axios";
 +
 +const { like, eachLike } = MatchersV3;
 +
 +describe("Login Contract", () => {
 +
 +  const provider = createPact("Auth-Service");
 +
 +  it("logs in successfully", async () => {
 +
 +    provider
 +      .given("A valid user exists")
 +      .uponReceiving("Login request")
 +      .withRequest({
 +        method: "POST",
 +        path: "/auth/login",
 +        body: { username: "john", password: "secret" }
 +      })
 +      .willRespondWith({
 +        status: 200,
 +        body: {
 +          accessToken: like("token"),
 +          refreshToken: like("refresh"),
 +          user: {
 +            id: like("u123"),
 +            roles: eachLike("ORDER.CREATE")
 +          }
 +        }
 +      });
 +
 +    await provider.executeTest(async mockServer => {
 +      const res = await axios.post(
 +        `${mockServer.url}/auth/login`,
 +        { username: "john", password: "secret" }
 +      );
 +      expect(res.status).toBe(200);
 +    });
 +  });
 +});
 +
 +</code>
 +
 +
 +
 +
 +====== Generated Pact Files ======
 +
 +Pacts output to:
 +
 +<code>
 +/pacts
 + ┗ BFF-Service-Auth-Service.json
 +
 +</code>
  
  
pact_contract_test.1767596861.txt.gz · Last modified: by pradnya