Web Development Testing 2 — Questions and Answers
Question 1: Which testing technique checks boundary values at the edges of input ranges?
- Equivalence partitioning
- Boundary value analysis (Correct answer)
- Decision table testing
- State transition testing
Correct answer: Boundary value analysis
Boundary value analysis tests values at the minimum, maximum, and just inside/outside the boundaries of valid input ranges.
Question 2: In Jest, what does the `beforeEach` hook do?
- Runs once before all tests in a file
- Runs before each individual test in a describe block (Correct answer)
- Runs after each individual test
- Cleans up mocks after each test
Correct answer: Runs before each individual test in a describe block
`beforeEach` executes a setup function before every individual test (`it`/`test`) within its scope.
Question 3: What is a test double that records calls and allows assertions on how it was used?
- Stub
- Fake
- Spy (Correct answer)
- Dummy
Correct answer: Spy
A spy wraps a real function and records information about its calls (arguments, return values, call count) for later assertions.
Question 4: Which HTTP status code range should a REST API return when a client sends an invalid request that fails validation?
- 2xx
- 3xx
- 4xx (Correct answer)
- 5xx
Correct answer: 4xx
4xx status codes indicate client-side errors; 400 Bad Request or 422 Unprocessable Entity are typical for validation failures.
Question 5: What does `screen.getByRole('button', { name: /submit/i })` test in React Testing Library?
- That a button element exists in the DOM by its CSS class
- That a button is accessible with the text 'submit' using ARIA roles (Correct answer)
- That a button's onClick handler is called on render
- That the form has exactly one submit button
Correct answer: That a button is accessible with the text 'submit' using ARIA roles
React Testing Library's `getByRole` queries the DOM using ARIA roles and accessible names, promoting accessibility-first testing.
Question 6: Which tool is commonly used to measure code coverage in a Node.js/Jest project?
- Istanbul (nyc) (Correct answer)
- Mocha
- Sinon
- Supertest
Correct answer: Istanbul (nyc)
Istanbul (also known as nyc) is the code coverage tool integrated into Jest by default via `--coverage`.
Question 7: What is the purpose of contract testing in a microservices architecture?
- To test the UI layer of each service independently
- To verify that services agree on the API shape they expect from each other (Correct answer)
- To load test all services simultaneously
- To enforce database schema consistency across services
Correct answer: To verify that services agree on the API shape they expect from each other
Contract testing (e.g., with Pact) ensures that a consumer's expectations of a provider's API match what the provider actually delivers.
Which testing technique checks boundary values at the edges of input ranges?