Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 5 OF 5 · 2 MIN READ

Testing Backend APIs

Testing a backend means unit tests for logic, integration tests against a real database, and API tests for endpoints. Interviewers ask how you structure tests, what to mock, and how tests run in CI.

What you will learn

  • Know the testing pyramid for backends
  • Test endpoints with Supertest
  • Use a real database in tests with containers
  • Decide what to mock and what not to

Key concepts

Unit test

Tests one function in isolation, fast and many. Good for pricing rules, validation, parsing.

Integration test

Tests your code with real dependencies such as PostgreSQL and Redis, usually in Docker (Testcontainers). Catches wrong SQL, missing migrations and transaction bugs that mocks hide.

Mock at the boundary

Mock what you do not own and cannot run locally: payment gateways, email providers, third-party APIs. Do not mock your own database layer in integration tests.

Step by step

Keep tests independent

  • Each test creates its own data
  • Reset with a transaction rollback or truncate between tests
  • No test depends on another test's order
  • Freeze time when logic depends on dates

Examples

An API test with Vitest and Supertest

import request from 'supertest'
import { describe, it, expect, beforeEach } from 'vitest'
import { app } from '../src/app.js'
import { resetDb, createUser, tokenFor } from './helpers.js'

describe('POST /orders', () => {
  beforeEach(resetDb)

  it('creates an order for a logged-in user', async () => {
    const user = await createUser()
    const res = await request(app).post('/orders').set('Authorization', 'Bearer ' + tokenFor(user)).send({ items: [{ sku: 'TSHIRT', qty: 2 }] })
    expect(res.status).toBe(201)
    expect(res.body.total).toBe(998)
  })

  it('rejects anonymous users', async () => {
    const res = await request(app).post('/orders').send({ items: [] })
    expect(res.status).toBe(401)
  })
})

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Write tests for the happy path, validation error, auth error and not-found case of one endpoint.
  • Set up Testcontainers with PostgreSQL and run migrations before the test suite.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Why export app separately from app.listen?

So tests can import the Express app and send requests in memory with Supertest without opening a real port, and so the server start file only handles listening and shutdown.

How do you test code that calls a payment gateway?

Put the gateway behind your own small interface, inject a fake in unit and API tests, and run a few contract or sandbox tests against the real provider separately.

Summary

Many fast unit tests, integration tests against a real database in containers, API tests with Supertest, and mocks only at external boundaries. Keep tests independent so they can run in parallel in CI.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus