Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

webhook-testing-fundamentals.md 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Webhook Testing Fundamentals
  2. ## Principle
  3. Webhook delivery is eventually consistent — your application fires HTTP callbacks asynchronously after events occur. Tests must poll until the expected webhook arrives or time out. The `@seontechnologies/playwright-utils` webhook module provides deterministic polling, typed matchers, rich timeout diagnostics, and cleanup strategies safe under `fullyParallel: true`.
  4. ## Rationale
  5. Webhook tests fail for four structural reasons:
  6. - **Eventually consistent**: Webhook delivery happens asynchronously — you cannot assert immediately after triggering an event
  7. - **Parallel journal pollution**: When multiple workers share the same mock server, a fast worker's teardown can delete records a slow worker is still polling
  8. - **Opaque timeouts**: A bare timeout tells you only that the webhook didn't arrive — it shows you nothing about what did arrive
  9. - **Cleanup drift**: Resetting the full journal in `afterEach` creates a race condition under `fullyParallel: true`
  10. The playwright-utils approach:
  11. - **Polling via `recurse`**: Uses Playwright's `expect.poll` under the hood — retries with configurable timeout and interval until a match is found
  12. - **Typed matchers**: `matchField`, `matchPartial`, `matchPredicate` — all must pass (AND semantics); matchers never throw on missing paths
  13. - **Rich timeout errors**: `WebhookTimeoutError` carries `totalReceived`, `receivedWebhooks`, and `matcherDetails` so you can see what arrived vs. what was expected
  14. - **Isolation via `startedAt`**: Each `WebhookRegistry` instance records its creation timestamp; polling only fetches webhooks received after that point, preventing leakage from prior tests
  15. - **Two cleanup strategies**: `full-reset` (resets entire journal) and `matched-only` (deletes only matched webhooks — parallel-safe when the provider supports delete-by-ID, e.g. WireMock)
  16. ## When to Use Webhook Tests
  17. | Scenario | Use webhook tests |
  18. | ----------------------------------------------------------------- | ------------------------- |
  19. | Application publishes events to external subscribers | ✅ Required |
  20. | Event-driven architecture with Kafka/event bus → webhook delivery | ✅ Required |
  21. | Payment, order, or notification side effects via webhooks | ✅ Required |
  22. | Testing that a webhook was NOT delivered | ✅ Verify via timeout |
  23. | Polling a status endpoint for eventual consistency | ❌ Use `recurse` directly |
  24. | Frontend receiving push notifications (WebSocket) | ❌ Different mechanism |
  25. ## Related Fragments
  26. - `webhook-module-setup.md` — Fixture wiring and cleanup strategies
  27. - `webhook-template-matchers.md` — matchField, matchPartial, matchPredicate
  28. - `webhook-waiting-querying.md` — waitFor, waitForCount, getReceived, drain pattern
  29. - `webhook-timeout-error.md` — WebhookTimeoutError debugging
  30. - `webhook-providers.md` — WireMock, MockServer, Mockoon, custom provider
  31. - `webhook-risk-guidance.md` — Risk-based guidance for TA and TD capabilities