QA Interview Hub · Knowledge base · Quizzes · Українська

API › Checklist: what to test in an endpoint

Beyond the response: side effects and system state

After a state-changing operation, separate requests check the system itself, not only the response: a 201 with correct JSON proves nothing. What had to happen: the record saved, related objects changed, a side effect exactly once. What must not: half-created records, others' data, duplicates. The owner comes from the token, and async effects are awaited by polling, not a pause.

API · Checklist

The response is 201, the JSON is perfect. Did the money really leave the account?

A 201 response with correct JSON does not yet prove that the endpoint works: it had to do something in the system, and that is what breaks most often. A transfer returned tr_91 but the recipient's balance did not change. An order was created but the stock did not decrease. A confirmation email went out twice. All of it is visible only when you look not at the response but at the system after it.

Below: what to check after every state-changing operation, why this is the layer most often skipped, and how to check side effects that arrive with a delay.

How it is built: three questions after every operation

An operation that changes state has consequences beyond the response. For each one ask: what else should have happened, and what should not have?

What should happen:

  • the record is saved: a repeated GET returns the same data, and after DELETE it is 404;
  • related objects changed: both balances after a transfer, the stock after an order;
  • a side effect appeared: an event in a queue, an email, a webhook, an audit log record, exactly once;
  • service fields are filled: createdAt, updatedAt, id, the owner comes from the token, not from the request body.

What should not happen:

  • a failed request changed nothing: after 400 or 409 there are no half-created records or debited money;
  • someone else's data did not change: a request of user A did not touch resources of B;
  • a repeat of the same request did not create a duplicate (idempotency, unique constraints).

The response only versus the response and the state

Typical

The response only

Status 201 and the shape of the JSON. It proves that the server answered, not that it did anything. A balance that did not change goes unnoticed.

Better

The response and the state

After the request we read the balances, the event and the record separately. The defect is seen in the test, not in a customer complaint.

Figure 1. The same five lines of a test, but the second approach checks what the request was made for.

Why it is built this way. An API only describes what happened: the response is formed by the handler code, while state changes are done by a separate layer (a database, a queue, an external service). They can diverge: the handler answered with success while the transaction rolled back or an event got lost. That is why the state has to be looked at with a separate request that does not depend on the response to the first one.

Example: a transfer that "passed" but did not change the balances

There was a test that checked only 201. After: we check the balances of both accounts and the event in the log.

test('transfer changes both balances and writes one audit record', async ({ request }) => {
  const a = await balance(request, 42);
  const b = await balance(request, 43);

  const res = await request.post('/v1/accounts/42/transfers', {
    headers: { 'Idempotency-Key': crypto.randomUUID() },
    data: { toAccount: '43', amountMinor: 5000, currency: 'UAH' },
  });
  expect(res.status()).toBe(201);
  const transfer = await res.json();

  expect(await balance(request, 42)).toBe(a - 5000);
  expect(await balance(request, 43)).toBe(b + 5000);

  // the event is written asynchronously: poll instead of a fixed sleep
  await expect
    .poll(async () => {
      const ev = await (await request.get('/v1/accounts/42/events')).json();
      return ev.filter((e: { transferId: string }) => e.transferId === transfer.id).length;
    })
    .toBe(1);
});

The balances are read with a separate GET, so the test does not rely on the response to the transfer. The event in the log is written asynchronously, so instead of a fixed sleep the test uses expect.poll: it repeats the function until the result is the expected one or the timeout runs out. The "exactly once" check catches both duplicate and missing events.

The negative side is just as important: a rejected request must leave no traces.

test('a rejected transfer changes nothing', async ({ request }) => {
  const a = await balance(request, 42);
  const b = await balance(request, 43);

  const res = await request.post('/v1/accounts/42/transfers', {
    data: { toAccount: '43', amountMinor: a + 1, currency: 'UAH' }, // more than the balance
  });
  expect(res.status()).toBe(409);

  expect(await balance(request, 42)).toBe(a);
  expect(await balance(request, 43)).toBe(b);
});

Here is a 409 for an amount above the balance (other APIs return 422: the contract sets the exact code), and right after it a check that both balances did not change. A test with an error that ends with a status check alone does not see half-debited money.

Limits and alternatives

  • State is most often checked through other endpoints of the same API (a GET of the created resource, balances, the event list). That does not tie the test to the internal schema.
  • Direct SQL or reading a queue is used when the API does not show what is needed. It is reliable but ties the test to the database structure: renaming a column breaks it with no change in the API.
  • Asynchronous effects (an email, a webhook) are checked by polling with a sensible timeout, or with a test mailbox or a webhook stub, not with a fixed pause.
  • A read (GET) does not change state, and the "state" layer here comes down to checking that the request changed nothing, for example a repeated GET returns the same.
  • Consistency in distributed systems can be only eventual, so "right after the request" the state may not be updated yet. This is agreed with the team, and the test waits by polling, not with an inflated sleep.

Common mistakes

  • A fixed pause instead of waiting for a condition. The test either fails on a slow environment or slows down for nothing. Better expect.poll or toPass.
  • Not checking the negative scenario. After an error the state did not change, but nobody looked, so half-created records pile up.
  • Taking the owner from the request body. A user will substitute someone else's id, so the owner is set from the token. The test checks that the value from the body is ignored.
  • Checking only one side. After a transfer the sender, the recipient and the log all change.
  • Relying on the response body instead of a separate read. A response may describe the intended state, not the actual one.

Check yourself

After DELETE /v1/orders/7 a 204 arrived. What do you check to make sure the deletion happened?

Answer

A repeated GET /v1/orders/7 must give 404. I also check the consequences: that the stock was returned, that the order is gone from the list, and that a repeated DELETE behaves per the contract (404 or 204). A 204 on its own only says the server considers the request done.

An event appears in the log a second or two later. Why is await page.waitForTimeout(3000) worse than expect.poll here?

Answer

A fixed pause is either too long and slows the suite, or too short and gives flaky failures on a slow environment. expect.poll repeats the check until the condition holds and has its own timeout, so the test waits exactly as long as needed.

In an interview. Junior: After an operation that changes data I check not only the response but also that the record was really saved through GET, and that after an error nothing changed. Middle/Senior: I look at three things: what should have happened (saving, related objects, side effects exactly once), what should not have happened (half-created records, someone else's data, duplicates), and that service fields, the owner in particular, come from the token. I read the state with separate requests and wait for asynchronous effects by polling, not with sleep. Direct SQL is for when the API does not show what is needed, because it ties the test to the database schema.

More articles in this chapter