Bird
0
0

You want to test a Remix loader that fetches user data from '/api/user'. How do you mock the API to return different user names for two tests without restarting the server each time?

hard📝 state output Q15 of 15
Remix - Testing
You want to test a Remix loader that fetches user data from '/api/user'. How do you mock the API to return different user names for two tests without restarting the server each time?
AChange the global handlers array and rerun setupServer() once
BCall server.close() and server.listen() before each test with new handlers
CUse server.use() with new handlers inside each test to override responses
DMock fetch globally with jest.fn() instead of msw
Step-by-Step Solution
Solution:
  1. Step 1: Understand dynamic mocking in msw

    msw allows overriding handlers per test using server.use() without restarting the server.
  2. Step 2: Compare options for best practice

    Use server.use() with new handlers inside each test to override responses uses server.use() to add new handlers dynamically; other options restart server or use different mocking tools.
  3. Final Answer:

    Use server.use() with new handlers inside each test to override responses -> Option C
  4. Quick Check:

    server.use() overrides handlers dynamically [OK]
Quick Trick: Use server.use() inside tests to change mocks on the fly [OK]
Common Mistakes:
MISTAKES
  • Restarting server unnecessarily each test
  • Modifying global handlers without reinitializing
  • Confusing msw with jest fetch mocks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Remix Quizzes