pact_contract_test
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| pact_contract_test [2026/01/05 07:09] – [Overview] pradnya | pact_contract_test [2026/01/05 08:27] (current) – [Overview] pradnya | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Pact contract test - Login API ====== | ====== Pact contract test - Login API ====== | ||
| - | |||
| ===== Overview ===== | ===== Overview ===== | ||
| Line 11: | Line 10: | ||
| * role array | * role array | ||
| * token fields | * token fields | ||
| + | |||
| + | POST /auth/login - Request | ||
| + | < | ||
| + | |||
| + | < | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | RESPONSE | ||
| + | < | ||
| + | |||
| + | < | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | Recommended Test Directory Structure | ||
| + | |||
| + | < | ||
| + | /tests | ||
| + | ┣ unit | ||
| + | ┃ ┣ api | ||
| + | ┃ ┣ domain | ||
| + | ┃ ┗ infrastructure | ||
| + | ┣ integration | ||
| + | ┃ ┣ api | ||
| + | ┃ ┗ data | ||
| + | ┗ pact | ||
| + | ┣ consumers | ||
| + | ┃ ┗ auth | ||
| + | | ||
| + | ┣ providers | ||
| + | ┃ ┗ auth | ||
| + | | ||
| + | ┗ helpers | ||
| + | ┗ pact-setup.ts | ||
| + | |||
| + | </ | ||
| + | |||
| + | **Clean separation** | ||
| + | |||
| + | ^Folder^Purpose| | ||
| + | |'' | ||
| + | |'' | ||
| + | |'' | ||
| + | |'' | ||
| + | |'' | ||
| + | |'' | ||
| + | |||
| + | Login Pact test lives in: | ||
| + | < | ||
| + | |||
| + | / | ||
| + | |||
| + | </ | ||
| + | |||
| + | **Helper Setup File** | ||
| + | |||
| + | < | ||
| + | pact/ | ||
| + | |||
| + | </ | ||
| + | |||
| + | Create pact | ||
| + | < | ||
| + | |||
| + | < | ||
| + | import path from " | ||
| + | import { PactV3 } from " | ||
| + | |||
| + | export function createPact(providerName: | ||
| + | return new PactV3({ | ||
| + | consumer: " | ||
| + | provider: providerName, | ||
| + | dir: path.resolve(process.cwd(), | ||
| + | logLevel: " | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | **login.pact.test.ts** <font 9px/ | ||
| + | |||
| + | < | ||
| + | import { createPact } from " | ||
| + | import { MatchersV3 } from " | ||
| + | import axios from " | ||
| + | |||
| + | const { like, eachLike } = MatchersV3; | ||
| + | |||
| + | describe(" | ||
| + | |||
| + | const provider = createPact(" | ||
| + | |||
| + | it(" | ||
| + | |||
| + | provider | ||
| + | .given(" | ||
| + | .uponReceiving(" | ||
| + | .withRequest({ | ||
| + | method: " | ||
| + | path: "/ | ||
| + | body: { username: " | ||
| + | }) | ||
| + | .willRespondWith({ | ||
| + | status: 200, | ||
| + | body: { | ||
| + | accessToken: | ||
| + | refreshToken: | ||
| + | user: { | ||
| + | id: like(" | ||
| + | roles: eachLike(" | ||
| + | } | ||
| + | } | ||
| + | }); | ||
| + | |||
| + | await provider.executeTest(async mockServer => { | ||
| + | const res = await axios.post( | ||
| + | `${mockServer.url}/ | ||
| + | { username: " | ||
| + | ); | ||
| + | expect(res.status).toBe(200); | ||
| + | }); | ||
| + | }); | ||
| + | }); | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ====== Generated Pact Files ====== | ||
| + | |||
| + | Pacts output to: | ||
| + | |||
| + | < | ||
| + | /pacts | ||
| + | ┗ BFF-Service-Auth-Service.json | ||
| + | |||
| + | </ | ||
pact_contract_test.1767596948.txt.gz · Last modified: by pradnya
