Network mocking
Tests that depend on data mock it. toets runs MSW handlers in a mock server on the test host. The app sends its API traffic to that server; matched requests get the handler's response, unmatched ones are proxied to the real backend. Because it is plain HTTP on the host, it works the same on web, emulators and simulators, without certificates.
Turn it on in the config with toets: { mock: true } (the network.mock capability):
import { test, expect, http, HttpResponse } from '@toets/test';
test('shows an empty state without favorites', async ({ app }) => {
app.network.use(http.get('*/api/favorites', () => HttpResponse.json([])));
await app.launch();
await expect(app.getByTestId('favorites-empty')).toBeVisible();
await expect(app.network).toHaveRequested({ method: 'GET', url: /\/api\/favorites$/ });
});How it works
- Each worker starts a mock server. Its URL reaches the app in the launch payload (
mockUrl), through the platform's launch channel: a URL parameter on web, an intent extra on Android, launch arguments on tvOS. - The app, when it finds
mockUrl, sends API requests there with the real origin in thex-toets-original-originheader. This is the one cooperation mocking needs from the app: app integration shows it for fetch and axios. - The mock server rebuilds the request as the app meant it (real origin plus path), so handlers match exactly what the real API would see.
- Handlers added with
app.network.use()apply to the current test only and reset after it. The most recently added handler wins. - Baseline handlers from the config apply to every test, under the per-test ones:
toets: { mock: { handlers: [...] } }.onUnhandledRequest: 'reject'fails unmatched requests (MOCK_UNHANDLED) instead of proxying them, for a hermetic suite. app.network.requests()andexpect(app.network).toHaveRequested()inspect what the app sent, mocked or not. Each test's traffic is attached to the report astoets-network.har.
Without the app-side change, the app keeps talking to its real backend and handlers never see a request. Matching, latency and failure helpers: writing handlers.
Baseline handlers
Responses every test needs (config, auth, feature flags) belong in the config, not in each test:
// playwright.config.ts
import { defineConfig, http, HttpResponse, platforms } from '@toets/test';
export default defineConfig({
app: { web: { url: 'http://localhost:8080' } },
toets: {
mock: {
handlers: [http.get('*/api/config', () => HttpResponse.json({ features: [] }))],
onUnhandledRequest: 'reject',
},
},
projects: [platforms.web()],
});A test's own app.network.use() handlers win over them. A project's toets.mock merges key by key over the top-level one.
Traps
- Add handlers before
launch()for data the app fetches at startup. Handlers added later only affect later requests. - A mock that does nothing usually means the app never routed the request. Check
app.network.requests(): an absent request went to the real host directly (native players and image loaders often do). - Assert the screen, then the request. A request assertion proves the app asked; only the screen proves it showed the result.
- Mock to make tests deterministic, not to test the backend. Keep a few unmocked smoke tests that exercise real responses.