Guides

Unit tests vs e2e tests — what's the difference and where to start

Illustration: on the left one gear under a magnifier, on the right a whole machine driving a route

An uncomfortable truth almost everyone runs into: you can have a hundred green tests and an app where the Buy button doesn't work.

That happens because tests come in different sizes, and the most popular kind — unit tests — checks pieces in isolation. Every gear is fine; the assembled machine won't drive. Which makes "unit or e2e" a practical question, not a theoretical one — especially when AI writes your tests, because unit tests are exactly what it writes most eagerly.

What each one checks

A unit test takes one function and checks it in a vacuum: given this input, expect this output. No database, no network, no browser. Does calculateTotal() apply the discount correctly, does it break on zero, what does it return for a negative number.

An e2e test (end-to-end) launches the whole app and impersonates a human: opens the page, types an email, clicks the button, waits for "Thank you". It doesn't care how things work inside — only that it worked from the outside.

Put plainly: unit asks "did I compute this right?", e2e asks "can a user actually get to the end?".

Table: how they differ

| Criterion | Unit test | E2E test | |---|---|---| | Catches | logic errors: maths, conditions, edge cases | a broken flow end to end: button, form, redirect, login | | Does NOT catch | that the button isn't clickable and data never arrived | which piece of logic computed the wrong number | | Speed | milliseconds, hundreds of tests in seconds | seconds to minutes per flow | | Stability | stable: same function, same answer | flaky: network, animations, timing | | When it fails | you see the exact line and function | you see what broke, not always where | | Maintenance cost | low | high: change the markup, fix the tests | | How well AI does | great: the code is visible, cases are predictable | so-so: it fumbles selectors and expectations |

The key row is the second one. A unit test physically cannot see that your frontend posts to the wrong endpoint, and an e2e test physically cannot tell you that a 12.5% discount rounds the wrong way. They cover different holes and don't replace each other.

So which should you write?

No fence-sitting, for someone who built an app with AI and wants it to stop falling apart:

Start with one or two e2e tests on the main flow — the one without which the product is pointless. Signup, checkout, form submit. It's cheap insurance against the most expensive embarrassment: "everything's green" while users can't get in.

Use unit tests on the nasty logic. Money, dates, discounts, limits, permissions — anywhere an off-by-one is easy and the failure is silent. Those tests take minutes to write and last for years.

What not to do: don't unit-test everything. A test for a function that concatenates two strings buys false confidence and breaks on every refactor. Tests are code, and code has to be maintained.

Where AI helps and where it hurts

AI writes unit tests almost for free: hand it a function, get a set of cases back, including ones you wouldn't have thought of. But that's also the trap — it tends to write tests for how the code behaves now, not how it should behave. If the function already has a bug, AI will happily freeze it as expected behaviour, and the test becomes a green guard standing over the error.

Hence the working trick: ask AI for a list of cases first, not for code. You can read a list and spot "no, this one should be different". With e2e it does worse — flows depend on markup and timing, so your first one nearly always needs hand-fixing. Same root cause as bugs in AI-written code: the model sees a fragment, not a running system.

What about integration tests?

The middle ground: several pieces together, but no browser. A call to your own API against a real database, say. Useful — but it's the third thing you add, once the two ends aren't enough.

How many tests are enough?

Chasing coverage percentages is pointless for a beginner. A workable bar: every flow whose failure costs money or reputation is covered by e2e; every function whose failure is silent is covered by a unit test. Everything else comes as things break — something broke, write a test so it can't break the same way twice.

What should run automatically?

Unit tests on every commit: they're fast and cheap. E2E on pushes to the main branch or before a release: they're slow and temperamental. That keeps the green check in CI fast while still exercising the critical path before production.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →