Web Development Testing 5 — Questions and Answers
Question 1: What is 'snapshot testing' as used in Jest?
- Taking a screenshot of the UI for visual comparison
- Serializing a component's rendered output and comparing it to a stored reference on future runs (Correct answer)
- Saving test results to a database for trend analysis
- Recording network requests for replay in future tests
Correct answer: Serializing a component's rendered output and comparing it to a stored reference on future runs
Jest snapshot testing serializes a component's output (often JSX/HTML) into a `.snap` file and fails if the output changes unexpectedly.
Question 2: Which Playwright locator is most resistant to CSS/styling changes when targeting elements?
- .locator('.btn-primary')
- .locator('#submit')
- .getByRole('button', { name: 'Submit' }) (Correct answer)
- .locator('button:nth-child(2)')
Correct answer: .getByRole('button', { name: 'Submit' })
Role-based locators use ARIA semantics rather than visual attributes, making them resilient to style changes and better for accessibility.
Question 3: What is the purpose of `afterAll` in Jest?
- Runs a cleanup function once after all tests in the describe block complete (Correct answer)
- Resets all mocks after every individual test
- Skips remaining tests if one fails
- Asserts that all expected calls were made
Correct answer: Runs a cleanup function once after all tests in the describe block complete
`afterAll` runs a teardown function exactly once after all tests in its scope have finished, useful for closing database connections or servers.
Question 4: In load testing, what does 'throughput' measure?
- The average response time per request
- The number of requests processed per unit of time (Correct answer)
- The percentage of requests that fail under load
- The memory consumed by the server during peak traffic
Correct answer: The number of requests processed per unit of time
Throughput measures how many requests (or transactions) a system can handle per second, indicating its processing capacity.
Question 5: What is the difference between a 'mock' and a 'stub' in testing terminology?
- Stubs verify behavior; mocks provide canned responses
- Mocks verify behavior (expectations); stubs provide canned responses without assertions (Correct answer)
- They are interchangeable terms with no practical difference
- Mocks replace entire modules; stubs replace single functions only
Correct answer: Mocks verify behavior (expectations); stubs provide canned responses without assertions
Stubs return pre-configured responses to make tests work, while mocks additionally assert that specific interactions occurred (they are pre-programmed with expectations).
Question 6: Which of the following correctly describes a 'regression test'?
- A test designed to find new features in the codebase
- A test that verifies previously working functionality still works after a code change (Correct answer)
- A performance test run before a major release
- A test written by a QA team without developer input
Correct answer: A test that verifies previously working functionality still works after a code change
Regression tests guard against old bugs reappearing by re-verifying that existing functionality remains intact after new changes.
Question 7: What does `supertest` help you test in a Node.js application?
- React component rendering
- HTTP endpoints by making requests against an Express/Koa app without starting a live server (Correct answer)
- Database query performance
- WebSocket connection handling
Correct answer: HTTP endpoints by making requests against an Express/Koa app without starting a live server
Supertest allows you to make HTTP assertions against an Express (or similar) app instance directly in your tests without binding to a network port.
What is 'snapshot testing' as used in Jest?