Skip to content

Asserting requests ​

Every request that reaches the mock server is recorded, mocked or proxied. Assert on behavior the user can not see: the right call was made, with the right body, once.

ts
import { test, expect, Key } from '@toets/test';

test('liking a video posts to the API once', async ({ app }) => {
  await app.launch();
  await app.focus.navigateTo(app.getByTestId('like-button'));
  await app.remote.press(Key.SELECT);

  await expect(app.network).toHaveRequested({ method: 'POST', url: '/v1/likes' });
  const [like] = await app.network.requests({ method: 'POST', url: '/v1/likes' });
  expect(JSON.parse(like!.body!)).toEqual({ videoId: 'v1' });
});

Prefer the retrying matcher over reading requests() right after an action: the request may still be in flight.

Filters ​

requests({ method, url }):

  • method is case-insensitive.
  • A string url uses the same rules as handler paths: relative paths match any origin, :params and * work, the query string is ignored.
  • A RegExp is tested against the full URL including the query string: { url: /\/search\?q=news/ }.

What a record holds ​

FieldNotes
urlThe URL the app meant: original origin + path + query
headersAs the app sent them, lowercase names
body, bodyEncodingUTF-8 text, or base64 for binary bodies
mockedtrue when a handler answered, false when proxied or failed
statusnull while in flight or when the connection was dropped
error{ code, message } when the request failed (see concepts)
responseStatus, headers and decoded body the app received
idSame value as the x-toets-request-id response header

Bodies above maxRecordedBodyBytes (1 MiB) are recorded truncated (bodyTruncated) and always forwarded in full.

HAR ​

MockServer.toHar(filter?) exports the recorded traffic as HAR 1.2 (open it in browser devtools or any HAR viewer). toets attaches each test's HAR to its report; failed exchanges have status: 0 or the error status plus _toets.error.

TV trap ​

A negative assertion ("never called /v1/ads") only proves the app did not call it through the mock server. Make sure every client in the app routes through mockUrl before relying on absence.