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
- 12.1 Testing strategy and what to test
- 12.2 Writing unit tests with Vitest and Jest
- 12.3 Mocks, spies, fakes and fake timers
- 12.4 Component testing with Testing Library
- 12.5 End-to-end testing with Playwright and Cypress
- 12.6 Flaky tests and test reliability
- 12.7 Snapshot, visual and accessibility testing
- 12.8 Coverage, CI and test suite performance
- 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
What does the testing pyramid recommend?
- Many unit tests, fewer integration tests, few E2E tests
- Manual testing only, for anything with a user interface
- Roughly equal numbers of every kind of test
- 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.
What does this Jest or Vitest test check? ```js test("adds", () => { expect(add(2, 3)).toBe(5); }); ```
- That `add(2, 3)` returns exactly `5`, compared with `Object.is`
- That `add` is called exactly 5 times
- That `add` is defined, regardless of what it returns
- 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.
What is a mock function, like `vi.fn()` or `jest.fn()`, used for?
- Measuring how long a function takes to run
- Running a test inside a real browser instance
- A fake dependency that records calls and returns set values
- 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(...)`.
What is the guiding principle of Testing Library?
- Use CSS class names as the main way to find elements
- Test every internal state variable of each component
- Render components to strings and compare the output
- 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.
What does an end-to-end (E2E) test do?
- Scans the CSS files for typos and unused rules
- Checks only that the database schema is correct
- Drives the whole app through the UI in a real browser
- 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.
What makes a test "flaky"?
- It takes longer than a minute to finish
- It passes or fails without any code change
- It fails on every run, without exception
- 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.
What is snapshot testing, and what's its main risk?
- Comparing output to a saved copy; big diffs get approved unread
- Testing database backups; it's far too slow to run often
- Recording network traffic; it leaks passwords into the repo
- 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.
Why is 100% code coverage not proof that code is correct?
- 100% coverage is impossible to reach in JavaScript
- It shows lines ran, not that the results were checked
- Coverage tools always miscount lines in JavaScript
- 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.
What is Storybook mainly used for?
- Building and viewing UI components in isolation
- Bundling the production app for deployment
- Writing project documentation in Markdown only
- Running backend integration tests against an API
Answer: A. Building and viewing UI components in isolation Stories double as test cases and living documentation.
What is the "testing trophy" idea popularized for front-end apps?
- Write a test only after a bug has reached production
- Focus most effort on integration tests that render components together
- Reach 100% line coverage before shipping anything at all
- 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.
More Front End
- HTML semantics & accessibility
- CSS: box model, Flexbox, Grid
- JavaScript fundamentals
- Async JavaScript & the event loop
- TypeScript
- React: components, hooks & rendering
- State management: Redux, Zustand, context
- Angular and Vue basics
- Browser internals & rendering
- Web performance
- Build tools & bundlers
- Front-end security
- Mobile & cross-platform