Frontend Developer · Focus area 3: Build tools and testing
FE8: Unit and component tests
Goal: write automatic tests that catch frontend bugs before users do: choosing what to test, unit tests for logic, component tests that act like a user, faking the network and time, and keeping tests trustworthy rather than brittle.
You'll need: your Kirana Express project with TypeScript from FE7, Vitest (or Jest), React Testing Library with user-event, and
Mock Service Worker (MSW). You met testing ideas in Module 39.
Where this is used: teams that release often depend on tests to change code without fear. A cart that miscalculates GST, or a checkout button that stops working after a refactor, costs money and trust. Tests catch these in seconds, every time code changes.
How it differs: backend tests check data in and data out. Frontend tests must also check what a person sees and does: text on screen, buttons, keyboard use, loading and error states, across many small interactions.
Time: reading about 2 hours · practice (the Try it tasks) about 4 hours · assignment about 4 to 5 hours.
Lessons
- 1What to test, and how muchReading 15–25 minutes, plus practice✓
- 2Unit tests for logicReading 15–25 minutes, plus practice✓
- 3Component tests that act like usersReading 15–25 minutes, plus practice✓
- 4Faking the network and timeReading 15–25 minutes, plus practice✓
- 5Trustworthy testsReading 15–25 minutes, plus practice✓
🛠️Assignment: A test suite for Kirana Express
This gives the multi-page app project the tests it needs.
- Write a test plan for the ten most important behaviours, choosing unit, component or E2E for each.
- Write unit tests for the cart total, delivery fee and offers, including thresholds and empty carts.
- Write component tests for the product card, cart, slot picker and address form, querying by role and label.
- Use MSW to test loading, success, empty, error and out-of-order states, and fake timers for an OTP countdown.
- Run the suite ten times, fix any flaky tests, and record runtime and coverage for key modules.
Self-check before you finish:
- [ ] Every money calculation has edge-case tests at its thresholds.
- [ ] No test finds elements by CSS class.
- [ ] Every data-loading screen has an error-state test.
- [ ] No test reads the real clock or calls a real API.
- [ ] Ten runs in a row all pass.
✅Module check
Explain in a few sentences, in your own words:
How do you build frontend tests that catch real bugs and stay trustworthy?
Show a good answer
A good answer mentions: mostly component tests, plus unit tests for tricky logic and a few E2E journeys, starting with money and the
main journey; plain functions with arrange-act-assert, edge cases and reproduce-then-fix for bugs; component tests that query by role,
label and text and use user-event, avoiding implementation details; MSW and fake timers to test loading, empty, error, slow and
out-of-order states; and fixing flaky and brittle tests, using coverage to find gaps rather than as a goal, and never skipping failing
tests to get green.
📚Official references
Official documentation and guidance for this module's topics, chosen on 2 November 2026. Tools, laws and standards change, so check the current version before you rely on a detail. If a link has moved, search the same site.
📝Module quiz
10 questions · 10 marks each · 100 marks in all · pass mark 70% · retry as often as you like ·
