The testing strategy for a full-stack app

Intermediate · 14 min

Decide which tests Taskboard gets at each layer, why most of them should cross a seam, and which bugs only a real database or browser can catch.

Before thisEnd-to-end type safetyError handling across the stackOptimistic updates and consistency

Lucas Paganini · 14 min

The testing strategy for a full-stack app

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Lucas PaganiniReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A test is worth its cost when it would fail for a reason a user would care about, so the useful question is which seam a bug would cross.
  • Tests that run the real route, validation and database together catch the failures that unit tests with mocks cannot, such as a foreign key violation turning into a 500.
  • Most of a full-stack suite should be integration tests at the API boundary, with a few end-to-end tests for the journeys that make money or lose data.
  • Static checks (types, lint, schema validation) are the cheapest layer and remove a whole class of tests you would otherwise write.
  • Every layer of mocks you add moves the test further from what runs in production, so mock only what you do not own, such as the payment provider or the clock.

Why a full-stack app needs a strategy

A single-layer project can get away with one style of test. A React component library gets component tests. A pure function gets unit tests. Taskboard has a browser client, an API, validation, SQL and a database, and each of those can be correct while the app is broken. The client can send { name: "x" } while the API expects { title: "x" }. The API can accept a task for project 99 and only discover at insert time that project 99 does not exist.

The core problem is that bugs live in the seams, and the seams are exactly what small isolated tests skip. A test strategy for a full-stack app is therefore less about “how many unit tests” and more about deciding which seams you cross on purpose.

Think of it as a smoke detector budget. You can put a detector in every room (cheap, narrow), one on every floor (broader), or have a person walk the whole house each night (expensive, slow, covers everything). You want all three, in very different quantities.

The layers

For Taskboard I think in four layers, cheapest first.

  1. Static checks. TypeScript, the linter, and Zod schemas shared between client and server (you built these in end-to-end type safety). They run in seconds and fail on every commit. A mistyped field name never reaches a test.
  2. Unit tests for logic with real branching and no I/O: sorting rules, a price calculation, a permissions function.
  3. Integration tests at the API boundary. Call the real route, with the real validation and a real database, and assert on the HTTP response and the rows.
  4. End-to-end tests driving a real browser against a running copy of the whole app.

The single most useful sentence in this topic comes from a talk Kent C. Dodds gave, linked under further viewing: “write tests, not too many, mostly integration”. That is a statement about where the confidence per minute of effort is highest.

A bug a mock cannot see

Here is the concrete case. Taskboard’s create-task route inserts a row. If the project does not exist, Postgres refuses the insert with error code 23503 (foreign key violation), and the route turns that into a 404:

try {
  const [row] = await db.insert(tasks).values({ projectId, title }).returning();
  return c.json(row, 201);
} catch (e: any) {
  if (e?.cause?.code === "23503" || e?.code === "23503")
    return c.json({ error: "no such project" }, 404);
  throw e;
}

A unit test with a mocked db can only return what you told it to return. If you forget the catch, the mock-based test still passes, and production answers 500 for every task posted to a deleted project. The integration test below goes through a real Postgres engine instead:

test("a task for a missing project is a 404, not a 500", async () => {
  expect((await post("/api/projects/99/tasks", { title: "x" })).status).toBe(404);
});

I ran this with Vitest 5.1.1, Hono 4.13.20, Drizzle 0.45.4 and PGlite 0.5.17 (an in-process build of Postgres). The three tests, including the 404 one, pass in under a second. The next lesson sets this up properly.

That is the whole argument for integration tests in one example. The mock proves your code calls the database the way you imagined. The real database proves what actually happens.

What to mock

Mock what you do not own, or what costs money or time. For Taskboard that is the payment provider, the email service, the system clock, and sometimes object storage. Do not mock your own database, your own validation, or your own router. Each time you replace one of those, the test passes a little more often and means a little less.

A useful check: if the test would still pass after you delete the route’s try/catch or rename a column, it is not protecting you from the bugs that happen in practice.

What belongs in end-to-end tests

Browser tests are slow, a little flaky, and expensive to debug, so spend them on journeys, not on features. For Taskboard:

  • Sign in, create a project, add a task, reload, see the task still there.
  • Submit the form with an empty title and see the server’s message.
  • Upload an attachment and download it again.

Three to ten tests like that are worth more than a hundred that click through every settings screen. Each one crosses every seam at once, so when one fails you know something is broken in the whole path, and the integration tests tell you where.

A budget that works

A rough distribution I use as a starting point, not a rule:

Layer Count for Taskboard Runs
Static everything every save and commit
Unit tens every commit
API integration one or more per route and error case every commit
End-to-end under ten journeys every pull request

The count matters less than the shape. If you notice a bug that reached production, ask which seam it crossed and add one test at that seam. A strategy is a record of what has actually broken, not a diagram you drew on day one.

Stack-specific testing already has homes in sibling roadmaps. For the React side, see testing components with React Testing Library. For the Node side, see testing HTTP APIs with Supertest and testing backend services with real dependencies. This roadmap only decides how they fit together.

What the video covers

Lucas Paganini’s “Static, Unit, Integration, and End-to-End Tests Explained - Software Testing Series #1” is about fourteen minutes, published July 2022. Its description lists static, unit, integration and end-to-end tests, why we need them and which ones to use, with code examples for each type and a GitHub repository to follow along. Its examples are not this roadmap’s stack, so the mapping onto an API, a database and a browser is this lesson’s own. I could not read a transcript, so I rely on the title and description. Kent C. Dodds’ talk “Write tests. Not too many. Mostly integration.” is linked as the source of the weighting idea, and the “Modern Software Engineering” video as a counterpoint that argues for going beyond the pyramid metaphor.

Where this fits

You now have a map of which tests go where. The layer with the best return is the API plus database, so the next lesson builds that suite step by step: integration tests for the API and database.

Check yourself

  1. 01Why did the 404 test for a missing project pass only against the real database?

    The 404 comes from Postgres rejecting the insert with a foreign key violation. A mocked database never raises that error, so the mock-based test would have passed while production returned a 500.

  2. 02Where should the bulk of Taskboard's tests sit?

    At the API boundary, calling the real route with a real database. One such test covers the validation, the handler, the query and the response shape together.

  3. 03What do end-to-end tests add that API tests cannot?

    They prove the browser, the client code and the server agree, such as the form posting the right body and the page showing the server's error message.

  4. 04When is a mock the right tool?

    When the dependency is outside your control or slow and costly, such as the payment provider, an email API or the system clock.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark