Stop 13 · Testing the Full StackTopics · 2/6

Unit Testing (Backend)

Part of the Full Stack Developer Roadmap

In short

You change one function at midnight before a deadline. How do you know you didn't break three others? Unit tests check each small piece on its own, in milliseconds.

Testing individual backend functions — validation logic, utility functions, service-layer code — in isolation from the database and network.

The blob character tests a single bulb in a tester beside a coiled string of lights.

Think of it like this

Testing each bulb first

Before stringing up Diwali lights, you test each bulb in a small tester. If the whole string fails later, you already know the bulbs are fine and the fault is elsewhere. A unit test checks one function by itself, with no database or network involved.

Watch it happen

Arrange, Act, Assert

Arrange
Act
Assert

1/6Arrange: build a cart with items worth 300 and 200 rupees, and a 10% coupon.

The big ideas

What you need to know

One behaviour per test

Each test checks one thing, like 'expired coupons are ignored'. When it fails, the name tells you what broke.

Fast and isolated

No database, files or network. Hundreds of unit tests should finish in seconds.

Pure functions are easiest

Functions that take inputs and return outputs, with no side effects, are a joy to test. Design for that.

Test the edges

Empty carts, zero, negative numbers, huge values. Bugs love the edges.

See it in code

Walk through the code

ts
1import { test } from "node:test";2import assert from "node:assert/strict";3import { calculateTotal } from "./cart.js";4 5test("applies a 10% coupon", () => {6  const cart = [{ price: 300 }, { price: 200 }];7  const total = calculateTotal(cart, { percentOff: 10, expired: false });8  assert.equal(total, 450);9});10 11test("ignores an expired coupon", () => {12  const cart = [{ price: 300 }, { price: 200 }];13  assert.equal(calculateTotal(cart, { percentOff: 10, expired: true }), 500);14});

1/3Node's built-in test runner and strict assertions. No extra packages needed.

Run it with node --test. The same Arrange, Act, Assert shape works in Jest, Vitest or Mocha.

Save yourself the pain

Common mistakes

Oops

Testing private helper details, so every refactor breaks the tests.

Do this instead

Test what the function promises: inputs and outputs. Then you can rewrite its insides freely.

Oops

One giant test that checks ten different things.

Do this instead

Split it. Small, well-named tests tell you exactly what broke without reading the code.

Your mission

Test a pure function

~25 min

Quick check

Which of these is a unit test?

Go deeper

  1. 1Write unit tests for 3-4 pure backend functions using Jest.
  2. 2Mock a database call so the test doesn't need a real database.
  3. 3Practice testing edge cases, not just the happy path.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge