JavaScript Testing 5 — Questions and Answers
Question 1: What is code coverage and which metric measures the percentage of code lines executed by tests?
- A quality score; branch coverage
- A measure of executed code; statement/line coverage (Correct answer)
- A test count metric; function coverage
- A performance metric; path coverage
Correct answer: A measure of executed code; statement/line coverage
Code coverage measures how much source code is executed during tests; statement/line coverage reports the percentage of code lines hit.
Question 2: Which Jest matcher should you use to test that an async function rejects with a specific error?
- expect(fn()).toThrow()
- await expect(promise).rejects.toThrow() (Correct answer)
- expect(fn).toReject()
- await expect(fn).throwsAsync()
Correct answer: await expect(promise).rejects.toThrow()
await expect(promise).rejects.toThrow() correctly handles the async rejection and asserts the thrown error type or message.
Question 3: What is the role of JSDOM in Jest tests?
- It compiles JSX during testing
- It provides a browser-like DOM environment in Node.js for testing UI code (Correct answer)
- It runs JavaScript faster than V8
- It mocks network requests automatically
Correct answer: It provides a browser-like DOM environment in Node.js for testing UI code
JSDOM simulates a browser DOM environment in Node.js, allowing Jest to test DOM manipulation code without a real browser.
Question 4: In Cypress, what is the purpose of `cy.intercept()`?
- To block all network requests during tests
- To stub, spy on, or modify HTTP requests and responses (Correct answer)
- To intercept user keyboard events
- To prevent test flakiness by adding automatic waits
Correct answer: To stub, spy on, or modify HTTP requests and responses
cy.intercept() allows you to stub network requests with mock responses or spy on requests to verify they were made.
Question 5: What is mutation testing in the context of JavaScript?
- Testing that objects cannot be mutated
- Automatically modifying source code to verify tests catch the changes (Correct answer)
- Testing mutable vs immutable state
- A form of fuzz testing for type mutations
Correct answer: Automatically modifying source code to verify tests catch the changes
Mutation testing introduces small code changes (mutations) and checks whether existing tests fail, measuring test suite quality.
Question 6: What does `jest.restoreAllMocks()` do differently from `jest.clearAllMocks()`?
- They are identical
- restoreAllMocks also restores original implementations of spied methods (Correct answer)
- clearAllMocks also removes mock modules
- restoreAllMocks only works with jest.fn()
Correct answer: restoreAllMocks also restores original implementations of spied methods
jest.restoreAllMocks() restores the original implementations of mocks created with jest.spyOn(), while clearAllMocks only clears usage data.
Question 7: Which testing pattern describes testing a unit in complete isolation by replacing all its dependencies with test doubles?
- Integration testing
- Solitary unit testing (Correct answer)
- Sociable unit testing
- Contract testing
Correct answer: Solitary unit testing
Solitary unit testing (also called isolated unit testing) replaces all dependencies with mocks/stubs so only the unit itself is tested.
What is code coverage and which metric measures the percentage of code lines executed by tests?