Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

pactjs-utils-overview.md 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # Pact.js Utils Overview
  2. ## Principle
  3. Use production-ready utilities from `@seontechnologies/pactjs-utils` to eliminate boilerplate in consumer-driven contract testing. The library wraps `@pact-foundation/pact` with type-safe helpers for provider state creation, PactV4 JSON interaction builders, verifier configuration, and request filter injection — working equally well for HTTP and message (async/Kafka) contracts.
  4. ## Rationale
  5. ### Problems with raw @pact-foundation/pact
  6. - **JsonMap casting**: Provider state parameters require `JsonMap` type — manually casting every value is error-prone and verbose
  7. - **Repeated builder lambdas**: PactV4 interactions often repeat inline callbacks with `builder.query(...)`, `builder.headers(...)`, and `builder.jsonBody(...)`
  8. - **Verifier configuration sprawl**: `VerifierOptions` requires 30+ lines of scattered configuration (broker URL, selectors, state handlers, request filters, version tags)
  9. - **Environment variable juggling**: Different env vars for local vs remote flows, breaking change coordination, payload URL matching
  10. - **Express middleware types**: Request filter requires Express types that aren't re-exported from Pact
  11. - **Bearer prefix bugs**: Easy to double-prefix tokens as `Bearer Bearer ...` in request filters
  12. - **CI version tagging**: Manual logic to extract branch/tag info from CI environment
  13. ### Solutions from pactjs-utils
  14. - **`createProviderState`**: One-call tuple builder for `.given()` — handles all JsonMap conversion automatically
  15. - **`toJsonMap`**: Explicit type coercion (null→"null", Date→ISO string, nested objects flattened)
  16. - **`setJsonContent`**: Curried callback helper for PactV4 `.withRequest(...)` / `.willRespondWith(...)` builders (query/headers/body)
  17. - **`setJsonBody`**: Body-only shorthand alias of `setJsonContent({ body })`
  18. - **`buildVerifierOptions`**: Single function assembles complete VerifierOptions from minimal inputs — handles local/remote/BDCT flows
  19. - **`buildMessageVerifierOptions`**: Same as above but for message/Kafka provider verification
  20. - **`handlePactBrokerUrlAndSelectors`**: Resolves broker URL and consumer version selectors from env vars with breaking change awareness
  21. - **`getProviderVersionTags`**: CI-aware version tagging (extracts branch/tag from GitHub Actions, GitLab CI, etc.)
  22. - **`createRequestFilter`**: Pluggable token generator pattern — prevents double-Bearer bugs by contract
  23. - **`noOpRequestFilter`**: Pass-through for providers that don't require auth injection
  24. ## Installation
  25. ```bash
  26. npm install -D @seontechnologies/pactjs-utils
  27. # Peer dependency
  28. npm install -D @pact-foundation/pact
  29. ```
  30. **Requirements**: `@pact-foundation/pact` >= 16.2.0, Node.js >= 18
  31. ## Available Utilities
  32. | Category | Function | Description | Use Case |
  33. | ----------------- | --------------------------------- | ---------------------------------------------------- | ---------------------------------------------------------------- |
  34. | Consumer Helpers | `createProviderState` | Builds `[stateName, JsonMap]` tuple from typed input | Consumer tests: `.given(...createProviderState(input))` |
  35. | Consumer Helpers | `toJsonMap` | Converts any object to Pact-compatible `JsonMap` | Explicit type coercion for provider state params |
  36. | Consumer Helpers | `setJsonContent` | Curried request/response JSON callback helper | PactV4 `.withRequest(...)` and `.willRespondWith(...)` builders |
  37. | Consumer Helpers | `setJsonBody` | Body-only alias of `setJsonContent` | Body-only `.willRespondWith(...)` responses |
  38. | Provider Verifier | `buildVerifierOptions` | Assembles complete HTTP `VerifierOptions` | Provider verification: `new Verifier(buildVerifierOptions(...))` |
  39. | Provider Verifier | `buildMessageVerifierOptions` | Assembles message `VerifierOptions` | Kafka/async provider verification |
  40. | Provider Verifier | `handlePactBrokerUrlAndSelectors` | Resolves broker URL + selectors from env vars | Env-aware broker configuration |
  41. | Provider Verifier | `getProviderVersionTags` | CI-aware version tag extraction | Provider version tagging in CI |
  42. | Request Filter | `createRequestFilter` | Express middleware with pluggable token generator | Auth injection for provider verification |
  43. | Request Filter | `noOpRequestFilter` | Pass-through filter (no-op) | Providers without auth requirements |
  44. ## Decision Tree: Which Flow?
  45. ```
  46. Is this a monorepo (consumer + provider in same repo)?
  47. ├── YES → Local Flow
  48. │ - Consumer generates pact files to ./pacts/
  49. │ - Provider reads pact files from ./pacts/ (no broker needed)
  50. │ - Use buildVerifierOptions with pactUrls option
  51. └── NO → Do you have a Pact Broker / PactFlow?
  52. ├── YES → Remote (CDCT) Flow
  53. │ - Consumer publishes pacts to broker
  54. │ - Provider verifies from broker
  55. │ - Use buildVerifierOptions with broker config
  56. │ - Set PACT_BROKER_BASE_URL + PACT_BROKER_TOKEN
  57. └── Do you have an OpenAPI spec?
  58. ├── YES → BDCT Flow (PactFlow only)
  59. │ - Provider publishes OpenAPI spec to PactFlow
  60. │ - PactFlow cross-validates consumer pacts against spec
  61. │ - No provider verification test needed
  62. └── NO → Start with Local Flow, migrate to Remote later
  63. ```
  64. ## Design Philosophy
  65. 1. **One-call setup**: Each utility does one thing completely — no multi-step assembly required
  66. 2. **Environment-aware**: Utilities read env vars for CI/CD integration without manual wiring
  67. 3. **Type-safe**: Full TypeScript types for all inputs and outputs, exported for consumer use
  68. 4. **Fail-safe defaults**: Sensible defaults that work locally; env vars override for CI
  69. 5. **Composable**: Utilities work independently — use only what you need
  70. ## Pattern Examples
  71. ### Example 1: Minimal Consumer Test
  72. ```typescript
  73. import { PactV3 } from '@pact-foundation/pact';
  74. import { createProviderState } from '@seontechnologies/pactjs-utils';
  75. const provider = new PactV3({
  76. consumer: 'my-frontend',
  77. provider: 'my-api',
  78. dir: './pacts',
  79. });
  80. it('should get user by id', async () => {
  81. await provider
  82. .given(...createProviderState({ name: 'user exists', params: { id: 1 } }))
  83. .uponReceiving('a request for user 1')
  84. .withRequest({ method: 'GET', path: '/users/1' })
  85. .willRespondWith({ status: 200, body: { id: 1, name: 'John' } })
  86. .executeTest(async (mockServer) => {
  87. const res = await fetch(`${mockServer.url}/users/1`);
  88. expect(res.status).toBe(200);
  89. });
  90. });
  91. ```
  92. ### Example 2: Minimal Provider Verification
  93. ```typescript
  94. import { Verifier } from '@pact-foundation/pact';
  95. import { buildVerifierOptions, createRequestFilter } from '@seontechnologies/pactjs-utils';
  96. const opts = buildVerifierOptions({
  97. provider: 'my-api',
  98. port: '3001',
  99. includeMainAndDeployed: true,
  100. stateHandlers: {
  101. 'user exists': async (params) => {
  102. await db.seed({ users: [{ id: params?.id }] });
  103. },
  104. },
  105. requestFilter: createRequestFilter({
  106. tokenGenerator: () => 'test-token-123',
  107. }),
  108. });
  109. await new Verifier(opts).verifyProvider();
  110. ```
  111. ## Key Points
  112. - **Import path**: Always use `@seontechnologies/pactjs-utils` (no subpath exports)
  113. - **Peer dependency**: `@pact-foundation/pact` must be installed separately
  114. - **Local flow**: No broker needed — set `pactUrls` in verifier options pointing to local pact files
  115. - **Remote flow**: Set `PACT_BROKER_BASE_URL` and `PACT_BROKER_TOKEN` env vars
  116. - **Breaking changes**: Set `includeMainAndDeployed: false` when coordinating breaking changes (verifies only matchingBranch)
  117. - **Builder helpers**: Use `setJsonContent` when you need query/headers/body together; use `setJsonBody` for body-only callbacks
  118. - **Type exports**: Library exports `StateHandlers`, `RequestFilter`, `JsonMap`, `JsonContentInput`, `ConsumerVersionSelector` types
  119. ## Related Fragments
  120. - `pactjs-utils-consumer-helpers.md` — detailed createProviderState, toJsonMap, setJsonContent, and setJsonBody usage
  121. - `pactjs-utils-provider-verifier.md` — detailed buildVerifierOptions and broker configuration
  122. - `pactjs-utils-request-filter.md` — detailed createRequestFilter and auth patterns
  123. - `contract-testing.md` — foundational contract testing patterns (raw Pact.js approach)
  124. - `test-levels-framework.md` — where contract tests fit in the testing pyramid
  125. ## Anti-Patterns
  126. ### Wrong: Manual VerifierOptions assembly when pactjs-utils is available
  127. ```typescript
  128. // ❌ Don't assemble VerifierOptions manually
  129. const opts: VerifierOptions = {
  130. provider: 'my-api',
  131. providerBaseUrl: 'http://localhost:3001',
  132. pactBrokerUrl: process.env.PACT_BROKER_BASE_URL,
  133. pactBrokerToken: process.env.PACT_BROKER_TOKEN,
  134. publishVerificationResult: process.env.CI === 'true',
  135. providerVersion: process.env.GIT_SHA || 'dev',
  136. consumerVersionSelectors: [{ mainBranch: true }, { deployedOrReleased: true }],
  137. stateHandlers: {
  138. /* ... */
  139. },
  140. requestFilter: (req, res, next) => {
  141. /* ... */
  142. },
  143. // ... 20 more lines
  144. };
  145. ```
  146. ### Right: Use buildVerifierOptions
  147. ```typescript
  148. // ✅ Single call handles all configuration
  149. const opts = buildVerifierOptions({
  150. provider: 'my-api',
  151. port: '3001',
  152. includeMainAndDeployed: true,
  153. stateHandlers: {
  154. /* ... */
  155. },
  156. requestFilter: createRequestFilter({ tokenGenerator: () => 'token' }),
  157. });
  158. ```
  159. ### Wrong: Importing raw Pact types for JsonMap conversion
  160. ```typescript
  161. // ❌ Manual JsonMap casting
  162. import type { JsonMap } from '@pact-foundation/pact';
  163. provider.given('user exists', { id: 1 as unknown as JsonMap['id'] });
  164. ```
  165. ### Right: Use createProviderState
  166. ```typescript
  167. // ✅ Automatic type conversion
  168. import { createProviderState } from '@seontechnologies/pactjs-utils';
  169. provider.given(...createProviderState({ name: 'user exists', params: { id: 1 } }));
  170. ```
  171. _Source: @seontechnologies/pactjs-utils library, pactjs-utils README, pact-js-example-provider workflows_