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:43] – [Overview] pradnyapact_contract_test [2026/01/05 08:27] (current) – [Overview] pradnya
Line 42: Line 42:
 Recommended Test Directory Structure Recommended Test Directory Structure
  
-<code>/tests  +<code> 
- ┣ unit  +/tests 
- ┃ ┣ api  + ┣ unit 
- ┃ ┣ domain  + ┃ ┣ api 
- ┃ ┗ infrastructure  + ┃ ┣ domain 
- ┣ integration  + ┃ ┗ infrastructure 
- ┃ ┣ api  + ┣ integration 
- ┃ ┗ data  + ┃ ┣ api 
- ┗ pact  + ┃ ┗ data 
-   ┣ consumers  + ┗ pact 
-   ┃ ┗ auth  +   ┣ consumers 
-   ┃   ┗ login.pact.test.ts  +   ┃ ┗ auth 
-   ┣ providers  +   ┃   ┗ login.pact.test.ts 
-   ┃ ┗ auth  +   ┣ providers 
-   ┃   ┗ verify-login.pact.test.ts  +   ┃ ┗ auth 
-   ┗ helpers  +   ┃   ┗ verify-login.pact.test.ts 
-      ┗ pact-setup.ts +   ┗ helpers 
 +      ┗ pact-setup.ts
  
 </code> </code>
Line 71: Line 72:
 |''pact/providers/'' |Optional — verification tests| |''pact/providers/'' |Optional — verification tests|
 |''pact/helpers/'' |Shared setup utilities| |''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.1767599023.txt.gz · Last modified: by pradnya