User Tools

Site Tools


pact_contract_test

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
pact_contract_test [2026/01/05 07:11] pradnyapact_contract_test [2026/01/05 08:27] (current) – [Overview] pradnya
Line 12: Line 12:
  
 POST /auth/login - Request POST /auth/login - Request
 + <font 9px/inherit;;inherit;;inherit>json</font>
  
-json+<code> 
 +
 + "username": "john", 
 + "password": "secret" 
 +}
  
-<code>{ \\ "username": "john", \\ "password": "secret" \\ } \\  
 </code> </code>
  
 RESPONSE RESPONSE
 + <font 9px/inherit;;inherit;;inherit>json</font>
  
-json+<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> <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> </code>
  
  
pact_contract_test.1767597061.txt.gz · Last modified: by pradnya