Files
playkit/docs/NETWORK.md
ilia eb0fa8b589
CI / release (pull_request) Has been skipped
CI / skip-ci-check (pull_request) Successful in 5s
CI / secret-scan (pull_request) Successful in 4s
CI / build-and-test (pull_request) Successful in 16s
Add network intercept/error monitor; document soak and Outline sync
- interceptNetworkCall + startNetworkErrorMonitor for page traffic
  (spy/mock + fail on silent 4xx/5xx), with unit tests
- docs/NETWORK.md, docs/IDEAS.md (OSS backlog), docs/OUTLINE.md
  (update Outline QA & Dev Playkit page on each release)
- Adoption pause: no other consumer repos until punimtag soak
2026-07-14 20:49:51 -04:00

64 lines
1.6 KiB
Markdown

# Network helpers (page traffic)
`ApiClient` *makes* HTTP calls from the test process. These helpers watch what the
**browser page** itself does during UI flows — spy/mock specific calls, and fail
tests that silently collect 4xx/5xx in the background.
## Spy / mock — `interceptNetworkCall`
Set up **before** the action that triggers the request:
```ts
import { interceptNetworkCall } from '@levkin/playkit';
const users = interceptNetworkCall({
page,
method: 'GET',
url: '**/api/users',
});
await page.goto('/users');
const { status, responseJson } = await users;
expect(status).toBe(200);
```
Stub a response:
```ts
const users = interceptNetworkCall({
page,
url: '**/api/users',
fulfillResponse: {
status: 200,
body: [{ id: 1, name: 'e2e' }],
},
});
await page.goto('/users');
await users;
await expect(page.getByText('e2e')).toBeVisible();
```
## Background errors — `startNetworkErrorMonitor`
Catches silent backend failures while the UI still looks fine:
```ts
import { startNetworkErrorMonitor } from '@levkin/playkit';
test('dashboard stays clean', async ({ page }) => {
const net = startNetworkErrorMonitor(page, {
excludePatterns: [/analytics\.google\.com/, 'sentry.io'],
});
try {
await page.goto('/dashboard');
await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
} finally {
net.assertNoErrors(); // throws with method/status/url list if any 4xx/5xx
}
});
```
Opt out of known 4xx paths via `excludePatterns` (substring or `RegExp`).
Call `assertNoErrors()` (or `stop()`) so the listener is removed.