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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Webhook Module Setup
  2. ## Principle
  3. Wire the provider once in a central fixtures file using the `webhookProviderFixture + webhookFixture + mergeTests` pattern. Tests that request `webhookRegistry` get automatic setup and teardown; tests that don't pay nothing (Playwright lazy fixture evaluation).
  4. ## Fixture Wiring Pattern
  5. ### WireMock Provider (recommended for most setups)
  6. The WireMock provider works with any backend that implements the `/__admin/requests` API format — not just actual WireMock. The playwright-utils sample app's Express backend uses this exact format.
  7. ```typescript
  8. // playwright/support/merged-fixtures.ts
  9. import { test as base, mergeTests } from '@playwright/test';
  10. import { test as webhookFixture } from '@seontechnologies/playwright-utils/webhook/fixtures';
  11. import { WireMockWebhookProvider } from '@seontechnologies/playwright-utils/webhook';
  12. import { API_URL } from '../config/local.config';
  13. // Lazy-initialized by Playwright — no cost for tests that don't request webhookRegistry.
  14. const webhookProviderFixture = base.extend<{
  15. webhookProvider: WireMockWebhookProvider;
  16. }>({
  17. webhookProvider: async ({ request }, use) => {
  18. const provider = new WireMockWebhookProvider(API_URL, request);
  19. await use(provider);
  20. },
  21. });
  22. const test = mergeTests(
  23. base,
  24. // ...your other fixtures...
  25. webhookFixture,
  26. webhookProviderFixture,
  27. );
  28. // Use matched-only cleanup project-wide: each test only deletes the webhooks it
  29. // matched, so a parallel worker's teardown cannot wipe the shared journal while
  30. // another test is still mid-flight (fullyParallel: true race condition).
  31. test.use({ webhookConfig: { cleanupStrategy: 'matched-only' } });
  32. export { test };
  33. ```
  34. This is the exact pattern used in the playwright-utils E2E suite (`playwright/support/merged-fixtures.ts`).
  35. ### MockServer Provider
  36. ```typescript
  37. import { MockServerWebhookProvider } from '@seontechnologies/playwright-utils/webhook';
  38. const webhookProviderFixture = base.extend<{
  39. webhookProvider: MockServerWebhookProvider;
  40. }>({
  41. webhookProvider: async ({ request }, use) => {
  42. await use(new MockServerWebhookProvider(API_URL, request));
  43. },
  44. });
  45. const test = mergeTests(base, /* ...other fixtures... */ webhookFixture, webhookProviderFixture);
  46. // MockServer has no delete-by-ID on log entries — use full-reset for explicit cleanup
  47. test.use({ webhookConfig: { cleanupStrategy: 'full-reset' } });
  48. ```
  49. ### Mockoon Provider
  50. ```typescript
  51. import { MockoonWebhookProvider } from '@seontechnologies/playwright-utils/webhook';
  52. const webhookProviderFixture = base.extend<{
  53. webhookProvider: MockoonWebhookProvider;
  54. }>({
  55. webhookProvider: async ({ request }, use) => {
  56. await use(new MockoonWebhookProvider(API_URL, request));
  57. },
  58. });
  59. const test = mergeTests(base, /* ...other fixtures... */ webhookFixture, webhookProviderFixture);
  60. // Mockoon has no delete-by-ID on log entries — use full-reset for explicit cleanup
  61. test.use({ webhookConfig: { cleanupStrategy: 'full-reset' } });
  62. ```
  63. ## Cleanup Strategy Decision
  64. | Strategy | Behaviour | When to choose |
  65. | ------------------------ | ------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------- |
  66. | `'full-reset'` (default) | Calls `provider.resetJournal()` — wipes the entire mock server journal | Safe only for serial execution or when each worker has an isolated provider instance |
  67. | `'matched-only'` | Calls `provider.deleteById(id)` for each webhook matched by `waitFor`/`waitForCount` | Required for `fullyParallel: true` with a shared journal **when the provider supports `deleteById`** (e.g. WireMock) |
  68. **The race condition under `fullyParallel: true`**: Worker A finishes and calls `resetJournal()`. Worker B is mid-poll waiting for its webhook. Worker A's reset just deleted Worker B's webhook — the poll times out with `WebhookTimeoutError`. Use `matched-only` to avoid this — but only when the provider supports `deleteById`.
  69. **MockServer and Mockoon limitation**: Neither supports `deleteById` — their implementations are no-ops. The `startedAt` timestamp filter isolates _reads_ inside `waitFor`/`waitForCount`, but `cleanup()` with `full-reset` still calls `resetJournal()`, which wipes the entire journal. This means the teardown race exists for these providers too under `fullyParallel: true`. For parallel suites with MockServer or Mockoon, either run serially (`workers: 1`) or provision an isolated mock server instance per worker.
  70. ## Fixture Lifecycle
  71. The fixture calls these in order:
  72. 1. `provider.setup?.()` — optional health check or stub registration
  73. 2. Tests run with `webhookRegistry` available
  74. 3. `registry.cleanup()` — deletes matched webhooks (`matched-only`) or resets journal (`full-reset`)
  75. 4. `provider.teardown?.()` — optional resource cleanup
  76. Both cleanup and teardown failures are caught and logged as warnings — they don't mask actual test failures.
  77. ## WebhookRegistryConfig Options
  78. ```typescript
  79. type WebhookRegistryConfig = {
  80. defaultTimeout?: number; // default: 30000 ms
  81. defaultInterval?: number; // default: 1000 ms
  82. cleanupStrategy?: 'matched-only' | 'full-reset'; // default: 'full-reset'
  83. };
  84. ```
  85. ## Related Fragments
  86. - `webhook-testing-fundamentals.md` — Why webhook tests are hard
  87. - `webhook-template-matchers.md` — Template building and matcher patterns
  88. - `webhook-providers.md` — WireMock, MockServer, Mockoon, custom provider details
  89. - `fixtures-composition.md` — mergeTests pattern