Delve into the core practices of mocking and stubbing within frontend testing. This quiz explores their roles, best use cases, and differences to boost your understanding of reliable and isolated test strategies in modern web development.
In the context of frontend testing, what is the primary reason for mocking a module, such as an HTTP request utility, during a unit test?
Explanation: Mocking allows you to replace real modules or functions with controlled test doubles, enabling isolation from unpredictable external factors and consistent test results. Improving visual rendering (option B) and smoother animations (option C) are not related to the logic or flow control achieved by mocking. Preventing browser crashes on large payloads (option D) is a performance concern, not directly addressed through mocking.
When testing a form submission handler, what distinguishes a stub from a mock in terms of their expected usage and behavior?
Explanation: Stubs focus on providing controlled outputs for certain functions, enabling predictable test results; mocks extend this by also tracking how those functions are called, such as the number of invocations or the arguments. Option B confuses functionality—stubs typically do not check arguments, and mocks do not rewrite source code. Option C is incorrect, as both stubs and mocks are meant to avoid real network interactions. Option D makes vague claims that do not capture their technical differences.
Given a user profile component that displays data from a backend, which approach is preferable if you want your test to focus solely on verifying the display logic with controlled data?
Explanation: Stubs are well-suited for providing consistent, predetermined data—ideal for focusing tests on how that data is displayed, rather than how it is fetched or generated. Randomly changing values through mocks (option B) would reduce test predictability. Allowing real backend calls (option C) makes tests less reliable and harder to isolate. Option D, injecting typos in endpoints, does not represent a proper testing approach and could cause false negatives.
What is a common risk when frontend tests do not use mocks or stubs for API calls, relying directly on the backend service?
Explanation: By not isolating the tests from real backend services, tests become vulnerable to network issues or changes in backend data, making them unreliable and flaky. Option B refers to UI performance, which is unrelated to backend communication. Option C is incorrect, as test scripts do not automatically skip execution for this reason. Option D, about form validation, does not connect to the use of mocks or stubs for API calls.
In a test where you need to ensure a search function is called exactly once when a button is clicked, which technique would best help confirm this behavior?
Explanation: Mocks can track how often a function is called, making them ideal for verifying invocation counts or usage patterns. Stubs (option B) do not typically record how they are used, and always returning errors may not test the desired behavior. Running tests without test doubles (option C) prevents precise verification of function calls. Overwriting global variables (option D) is unrelated to tracking function invocations.