Skip to content

Types of Tests

Unit, integration, functional, and end-to-end tests explained with when to use each type.

What you'll learn

  • The difference between unit, integration, and end-to-end tests
  • What functional, smoke, and regression tests are
  • When each test type gives the best return on investment
  • How to balance speed and confidence across test types

Concept

Unit Tests

A unit test exercises the smallest meaningful piece of code — usually a single function or method — in isolation. Dependencies are replaced with test doubles so that the test only checks the unit's own logic. Unit tests are fast (often thousands per second), deterministic, and pinpoint failures precisely. They are the foundation of your suite.

Integration Tests

An integration test verifies that two or more modules work correctly together. Instead of stubbing every dependency, you let real modules interact, often with a real database or a real file system in a controlled state. Integration tests are slower than unit tests but catch wiring bugs — wrong function signatures, mismatched data shapes, transaction boundaries — that unit tests miss.

End-to-End (E2E) Tests

An end-to-end test drives the whole application from the outside, typically by automating a browser the way a real user would: clicking buttons, filling forms, and asserting that the page shows the right result. E2E tests give the highest confidence that the system truly works, but they are slow, brittle, and expensive to maintain, so you keep them few and focused on critical journeys.

Other Useful Types

Smoke tests run after a deploy to check that the system boots and core paths work. Regression tests are added specifically to prove a previously fixed bug stays fixed. Functional tests verify business requirements without caring about implementation details. Choosing the right type for each scenario is a key testing skill — you will rarely rely on just one.

In Practice

A practical rule of thumb: test a piece of logic at the lowest layer that can observe it. If a calculation can be checked with a unit test, do that — it runs in a millisecond and pinpoints the failure. If two modules must agree on a data shape, write an integration test that lets them run together. If the only way to know the feature works is to click through it like a user, write one E2E test for that journey. Mixing types is normal and healthy; what you want to avoid is putting every assertion in the slowest, most brittle layer. Tag your tests by type (@unit, @integration, @e2e) so you can run a fast subset locally and the full suite in CI. This layered approach keeps your feedback loop tight while still giving you the confidence that the whole system hangs together.

Example

              // Unit test: pure function in isolation
function discount(price, percent) {
  return price - price * (percent / 100);
}
assert.equal(discount(100, 20), 80);

// Integration test: repository + in-memory database together
const db = new InMemoryDb();
const repo = new UserRepository(db);
await repo.save({ id: 1, name: "Ada" });
const found = await repo.findById(1);
assert.equal(found.name, "Ada");

// E2E test (conceptual): drive a real HTTP endpoint
const res = await fetch("/api/users/1");
assert.equal(res.status, 200);
            

The same feature can be tested at three levels. Unit tests check the math, integration tests check the data layer, and E2E tests check the HTTP contract from the outside.

Try it

Common mistakes

The mistake

Calling a test 'unit' while it hits a real database

The fix

If the test touches external systems, it is an integration test. Replace the database with a fake or move the test to the integration suite.

The mistake

Using E2E tests for logic that a unit test could cover

The fix

Push logic down to pure functions and unit-test them. Reserve E2E tests for user-visible journeys only.

Practice Types of Tests

6 exercises

Practice Types of Tests

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors