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 08:17] pradnyapact_contract_test [2026/01/05 08:27] (current) – [Overview] pradnya
Line 75: Line 75:
 Login Pact test lives in: Login Pact test lives in:
 <code> <code>
-/tests/pact/consumers/auth/login.pact.test.ts + 
 +/tests/pact/consumers/auth/login.pact.test.ts 
 </code> </code>
  
-Helper Setup File+**Helper Setup File** 
 + 
 +<code> 
 +pact/helpers/pact-setup.ts
  
-<code>pact/helpers/pact-setup.ts 
 </code> </code>
 +
 +Create pact
  <font 9px/inherit;;inherit;;inherit>ts</font>  <font 9px/inherit;;inherit;;inherit>ts</font>
  
 <code> <code>
 +import path from "path";
 +import { PactV3 } from "@pact-foundation/pact";
  
-import { createPact } from "../../helpers/pact-setup"; \\ +export function createPact(providerName: string) { 
-import { MatchersV3 } from "@pact-foundation/pact"; \\+  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"; import axios from "axios";
  
Line 98: Line 120:
   it("logs in successfully", async () => {   it("logs in successfully", async () => {
  
-    provider \\ +    provider 
-      .given("A valid user exists"\\ +      .given("A valid user exists"
-      .uponReceiving("Login request"\\ +      .uponReceiving("Login request"
-      .withRequest({ \\ +      .withRequest({ 
-        method: "POST", \\ +        method: "POST", 
-        path: "/auth/login", \\ +        path: "/auth/login", 
-        body: { username: "john", password: "secret"\\ +        body: { username: "john", password: "secret"
-      }) \\ +      }) 
-      .willRespondWith({ \\ +      .willRespondWith({ 
-        status: 200, \\ +        status: 200, 
-        body: { \\ +        body: { 
-          accessToken: like("token"), \\ +          accessToken: like("token"), 
-          refreshToken: like("refresh"), \\ +          refreshToken: like("refresh"), 
-          user: { \\ +          user: { 
-            id: like("u123"), \\ +            id: like("u123"), 
-            roles: eachLike("ORDER.CREATE"\\ +            roles: eachLike("ORDER.CREATE"
-          } \\ +          } 
-        } \\+        }
       });       });
  
-    await provider.executeTest(async mockServer => { \\ +    await provider.executeTest(async mockServer => { 
-      const res = await axios.post( \\ +      const res = await axios.post( 
-        `${mockServer.url}/auth/login`, \\ +        `${mockServer.url}/auth/login`, 
-        { username: "john", password: "secret"\\ +        { username: "john", password: "secret"
-      ); \\ +      ); 
-      expect(res.status).toBe(200); \\ +      expect(res.status).toBe(200); 
-    }); \\ +    }); 
-  }); \\ +  }); 
-}); \\ +});
  
 </code> </code>
  
  
 +
 +
 +====== Generated Pact Files ======
 +
 +Pacts output to:
 +
 +<code>
 +/pacts
 + ┗ BFF-Service-Auth-Service.json
 +
 +</code>
  
  
pact_contract_test.1767601067.txt.gz · Last modified: by pradnya