Web Development Testing 3 — Questions and Answers
Question 1: What is 'test flakiness' in the context of automated testing?
- Tests that fail because of missing dependencies
- Tests that produce inconsistent pass/fail results without code changes (Correct answer)
- Tests that take longer than 10 seconds to run
- Tests written without assertions
Correct answer: Tests that produce inconsistent pass/fail results without code changes
Flaky tests are non-deterministic — they sometimes pass and sometimes fail on the same code, often due to timing issues or shared state.
Question 2: In Cypress, what does `cy.intercept()` allow you to do?
- Intercept and stub network requests made by the app under test (Correct answer)
- Intercept keyboard events before they reach the browser
- Mock the browser's localStorage API
- Capture console errors during a test run
Correct answer: Intercept and stub network requests made by the app under test
`cy.intercept()` lets you spy on, stub, or modify HTTP requests and responses at the network level during Cypress tests.
Question 3: Which of the following best describes 'smoke testing'?
- Testing every possible user input combination
- A quick set of tests to verify the most critical features work before deeper testing (Correct answer)
- Performance testing under extreme load
- Testing the application in a production environment
Correct answer: A quick set of tests to verify the most critical features work before deeper testing
Smoke testing is a shallow, fast check that the build is stable enough for further testing — it confirms core functionality works.
Question 4: What does TDD (Test-Driven Development) require you to write first?
- The implementation code
- A failing test (Correct answer)
- Documentation
- A passing integration test
Correct answer: A failing test
TDD follows a red-green-refactor cycle: write a failing test first, then write the minimal code to make it pass, then refactor.
Question 5: Which Playwright method waits for a network response matching a URL before resolving?
- page.waitForTimeout()
- page.waitForResponse() (Correct answer)
- page.waitForSelector()
- page.waitForLoadState()
Correct answer: page.waitForResponse()
`page.waitForResponse()` waits until a network response matching the given URL or predicate is received, preventing race conditions.
Question 6: What is the key difference between `jest.fn()` and `jest.spyOn()`?
- `jest.fn()` creates a new mock function; `jest.spyOn()` wraps an existing object method (Correct answer)
- `jest.fn()` only works with async functions; `jest.spyOn()` works with sync
- `jest.spyOn()` creates a permanent replacement; `jest.fn()` is temporary
- They are identical in functionality
Correct answer: `jest.fn()` creates a new mock function; `jest.spyOn()` wraps an existing object method
`jest.fn()` creates a standalone mock from scratch, while `jest.spyOn()` replaces an existing method on an object with a mock that can be restored.
Question 7: In the context of web testing, what is a 'headless' browser?
- A browser running without HTTP headers
- A browser that runs without a graphical user interface (Correct answer)
- A browser with JavaScript disabled
- A browser instance shared across test workers
Correct answer: A browser that runs without a graphical user interface
A headless browser runs without rendering a visible GUI, making it faster and suitable for automated testing in CI environments.
What is 'test flakiness' in the context of automated testing?