Web Development Testing 4 — Questions and Answers
Question 1: Which assertion library method in Chai verifies that a value is strictly equal using `===`?
- .equal() (Correct answer)
- .eql()
- .deepEqual()
- .include()
Correct answer: .equal()
Chai's `.equal()` uses strict equality (`===`), while `.eql()` performs a deep equality check for objects and arrays.
Question 2: What is the primary goal of mutation testing?
- To test database schema migrations
- To measure how well your tests detect deliberate bugs introduced into the code (Correct answer)
- To test code that handles random input
- To evaluate performance degradation over time
Correct answer: To measure how well your tests detect deliberate bugs introduced into the code
Mutation testing tools (like Stryker) introduce small code changes (mutations) and check if your tests catch them, revealing gaps in test quality.
Question 3: In end-to-end testing, what is a 'Page Object Model' (POM)?
- A JSON schema defining page structure for SEO
- A design pattern that encapsulates page interactions into reusable classes (Correct answer)
- A CSS module pattern for test environments
- A way to snapshot the DOM for visual regression
Correct answer: A design pattern that encapsulates page interactions into reusable classes
The Page Object Model abstracts UI interactions into dedicated classes, reducing code duplication and making tests easier to maintain.
Question 4: What does `act()` in React Testing Library help with?
- Simulating network delays in tests
- Ensuring all state updates and effects are applied before making assertions (Correct answer)
- Mocking React context providers
- Running tests in strict mode
Correct answer: Ensuring all state updates and effects are applied before making assertions
`act()` wraps code that triggers React state updates, ensuring the component re-renders and effects complete before assertions are evaluated.
Question 5: Which HTTP method should an API endpoint test use to verify idempotent resource deletion?
- POST
- PATCH
- DELETE (Correct answer)
- PUT
Correct answer: DELETE
DELETE is the idempotent method for removing resources; calling it multiple times should result in the same state (resource gone).
Question 6: In a CI pipeline, at which stage is it most appropriate to run unit tests?
- After deployment to production
- Before building the application artifact (Correct answer)
- Only on the main branch after merging
- After integration tests pass
Correct answer: Before building the application artifact
Unit tests are fast and should run early in CI (before building) to fail fast and give developers immediate feedback on broken logic.
Question 7: What does the `--watch` flag do when running Jest?
- Increases test timeout thresholds
- Re-runs tests automatically when source files change (Correct answer)
- Outputs coverage reports to a file
- Runs tests in parallel across all CPU cores
Correct answer: Re-runs tests automatically when source files change
Jest's `--watch` mode monitors the file system and re-runs relevant tests whenever you save a file, enabling a fast feedback loop during development.
Which assertion library method in Chai verifies that a value is strictly equal using `===`?