Unit, integration, and E2E tests explained
the test pyramid.
unit tests isolate one function with dependencies mocked, integration tests exercise several units together (route plus DB), E2E tests drive the whole running system.
What's really being asked
Whether you understand the scope, cost, and purpose of each test layer and can place them on the test pyramid.
The full answer
Unit tests verify the smallest pieces, a single function or class, in isolation, with external collaborators like the database or other modules mocked or stubbed. They are fast, deterministic, and you write many of them. Integration tests verify that several components work together correctly, for example an Express route handler talking to a real test database or another service, catching bugs that live in the seams between units. They are slower and fewer. End-to-end tests drive the entire running application the way a real client would, sending HTTP requests to a deployed instance or automating a browser through Supertest, Playwright, or Cypress, validating complete user journeys. They give the most confidence but are slowest and most brittle. The test pyramid recommends many unit tests, fewer integration tests, and the fewest E2E tests.
The mistakes people make
Calling a route test that touches a real DB a unit test, treating all tests as interchangeable, or writing only E2E tests because they feel most realistic.
What usually comes next
Where mocking belongs, why the pyramid shape matters, flaky E2E tests, and how Supertest fits integration testing.
A concrete example
Unit: test a formatPrice(cents) utility returns 12.50 for 1250, no DB involved. Integration: send a POST to /users via Supertest against a test database and assert a row was created. E2E: automate a browser to register, log in, and see the dashboard, exercising frontend, API, and database together as a user would.
Interview question
You write a test that sends an HTTP request to a route handler which queries a real test database. Which category is this?
- a.An integration test, because it exercises the route and database togetherCorrect
- b.A unit test, because it targets one route
- c.An end-to-end test, because it uses HTTP
- d.A smoke test, because it touches the database
Why? this is the answer
Exercising multiple components together, the route plus a real database, through the API layer is an integration test. It is not a unit test (dependencies are not isolated) and not full E2E (it does not drive the entire deployed system through a client).
Just read this? Test yourself on what you have been reading.
Read the original → blog.logrocket.com
- #testing
- #unit-tests
- #integration-tests
- #e2e
- #test-pyramid
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on testing — each one lists the topics its interview covers.
See open roles