Front-end testing: unit, component and end-to-end tests – Front End interview prep

261 interview-style multiple-choice questions on Front-end testing: unit, component and end-to-end tests, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 59 key terms with the flashcards first, then test yourself.

Take the practice test · Flashcards · All study tools

What it covers

  1. 12.1 Testing strategy and what to test
  2. 12.2 Writing unit tests with Vitest and Jest
  3. 12.3 Mocks, spies, fakes and fake timers
  4. 12.4 Component testing with Testing Library
  5. 12.5 End-to-end testing with Playwright and Cypress
  6. 12.6 Flaky tests and test reliability
  7. 12.7 Snapshot, visual and accessibility testing
  8. 12.8 Coverage, CI and test suite performance
  9. 12.9 Testing hooks, forms, routing and data fetching

Key terms

12.1 Testing strategy and what to test

Testing pyramid
Many unit tests, fewer integration tests, few E2E tests
Flaky test
A test with inconsistent results without code changes
TDD
Test-driven development: red, green, refactor
Code coverage
The share of code executed during tests
Test seam
Place to swap in a test double
Test factory
Helper building valid objects with defaults
Deterministic test
Same result on every run
Test oracle
Source of truth for correct output

12.2 Writing unit tests with Vitest and Jest

Assertion
A check that a value meets an expectation
Mock
A fake replacing a real dependency in tests
Spy
A wrapper that records how a function is called
Fake timers
Test-controlled clock for timer-based code
describe
Groups tests with shared scope
test.each
Table-driven test cases
toEqual
Deep equality matcher
Browser mode
Vitest running tests in a real browser

12.3 Mocks, spies, fakes and fake timers

Module mock
Replacing a whole module in tests
MSW
Mock Service Worker, network-level mocking

12.4 Component testing with Testing Library

Testing Library
Tools for testing UI the way users interact with it
jsdom
A JavaScript implementation of the DOM for Node tests
findBy query
Async query that waits for an element to appear
userEvent
Library that simulates realistic user interactions
Query priority
Roles first, then labels, text and finally test IDs
jest-dom
Extra DOM matchers like `toBeInTheDocument`
within
Scopes queries to one element
Custom render
A `render` wrapper that adds providers
user-event
Simulates full user interactions

12.5 End-to-end testing with Playwright and Cypress

E2E test
A test that exercises the whole app through the UI
Auto-waiting
Waiting automatically for elements to be ready before acting
Page Object
Class wrapping a page's locators and actions
Storage state
Saved cookies and storage reused to skip logins
Web-first assertion
A Playwright assertion that retries until it passes
Browser context
An isolated browser session, like a new profile
codegen
Records actions into Playwright test code
Sharding
Splitting a suite across machines
cy.intercept
Cypress API to spy on or stub requests

12.6 Flaky tests and test reliability

Quarantine
Moving a flaky test out of the blocking suite until it's fixed
Flake rate
How often a test fails then passes on retry for one commit
Hermetic test
Controls all its inputs, with no outside dependencies
Heisenbug
A bug that changes when you try to observe it
Shuffled order
Running tests in random order to find order dependence
--repeat-each
Runs a Playwright test many times to reproduce flakiness

12.7 Snapshot, visual and accessibility testing

Inline snapshot
A snapshot stored in the test file
Property matcher
Lets a snapshot field match by type
Baseline
The approved image a visual test compares against
Masking
Covering changing areas in a screenshot
ARIA snapshot
A snapshot of the accessibility tree
axe-core
An engine that checks pages against accessibility rules

12.8 Coverage, CI and test suite performance

Branch coverage
Whether each side of every condition ran
Coverage threshold
A minimum coverage percentage that fails the run
Patch coverage
Coverage of only the lines a PR changes
happy-dom
A faster, less complete DOM for tests
JUnit reporter
XML test results that CI can parse
--bail
Stops a test run after N failures

12.9 Testing hooks, forms, routing and data fetching

renderHook
Renders a hook in a test component
wrapper
A component that wraps a hook or component in providers
Memory router
A router that keeps history in memory for tests
shallowMount
Vue Test Utils mount with stubbed children
toHaveValue
Checks a form control's current value

Sample practice questions

  1. What does the testing pyramid recommend?

    1. Many unit tests, fewer integration tests, few E2E tests
    2. Manual testing only, for anything with a user interface
    3. Roughly equal numbers of every kind of test
    4. Mostly end-to-end tests, since they're closest to real use

    Answer: A. Many unit tests, fewer integration tests, few E2E tests Fast, focused tests give quick feedback; a few E2E tests cover the critical journeys.

  2. What does this Jest or Vitest test check? ```js test("adds", () => { expect(add(2, 3)).toBe(5); }); ```

    1. That `add(2, 3)` returns exactly `5`, compared with `Object.is`
    2. That `add` is called exactly 5 times
    3. That `add` is defined, regardless of what it returns
    4. That `add(2, 3)` returns any truthy value

    Answer: A. That `add(2, 3)` returns exactly `5`, compared with `Object.is` `toBe` is strict identity; use `toEqual` for comparing objects by value.

  3. What is a mock function, like `vi.fn()` or `jest.fn()`, used for?

    1. Measuring how long a function takes to run
    2. Running a test inside a real browser instance
    3. A fake dependency that records calls and returns set values
    4. Generating random test data for each test run

    Answer: C. A fake dependency that records calls and returns set values You can then assert with `expect(fn).toHaveBeenCalledWith(...)`.

  4. What is the guiding principle of Testing Library?

    1. Use CSS class names as the main way to find elements
    2. Test every internal state variable of each component
    3. Render components to strings and compare the output
    4. The more tests resemble real use, the more confidence they give

    Answer: D. The more tests resemble real use, the more confidence they give Query elements the way users find them: by role, label and text.

  5. What does an end-to-end (E2E) test do?

    1. Scans the CSS files for typos and unused rules
    2. Checks only that the database schema is correct
    3. Drives the whole app through the UI in a real browser
    4. Tests one pure function in isolation from the rest

    Answer: C. Drives the whole app through the UI in a real browser E2E tests catch integration problems but are slower and more brittle.

  6. What makes a test "flaky"?

    1. It takes longer than a minute to finish
    2. It passes or fails without any code change
    3. It fails on every run, without exception
    4. It tests more than one function at a time

    Answer: B. It passes or fails without any code change Flaky tests erode trust; fix the cause rather than adding blind retries.

  7. What is snapshot testing, and what's its main risk?

    1. Comparing output to a saved copy; big diffs get approved unread
    2. Testing database backups; it's far too slow to run often
    3. Recording network traffic; it leaks passwords into the repo
    4. Screenshotting the developer's screen; it uses too much disk

    Answer: A. Comparing output to a saved copy; big diffs get approved unread Keep snapshots small and focused so diffs are meaningful.

  8. Why is 100% code coverage not proof that code is correct?

    1. 100% coverage is impossible to reach in JavaScript
    2. It shows lines ran, not that the results were checked
    3. Coverage tools always miscount lines in JavaScript
    4. Coverage measures performance, not correctness

    Answer: B. It shows lines ran, not that the results were checked A test with no assertions can still produce full coverage.

  9. What is Storybook mainly used for?

    1. Building and viewing UI components in isolation
    2. Bundling the production app for deployment
    3. Writing project documentation in Markdown only
    4. Running backend integration tests against an API

    Answer: A. Building and viewing UI components in isolation Stories double as test cases and living documentation.

  10. What is the "testing trophy" idea popularized for front-end apps?

    1. Write a test only after a bug has reached production
    2. Focus most effort on integration tests that render components together
    3. Reach 100% line coverage before shipping anything at all
    4. Skip unit tests and rely entirely on end-to-end tests

    Answer: B. Focus most effort on integration tests that render components together It argues integration tests give the best confidence per cost for UI code.

Practice all 261 questions

More Front End