Mocking Pattern

Mock dependencies effectively using Vitest mock functions, module mocking, timer control, MSW for HTTP requests, and Prisma mocking.

What's Included#

  • Mock functions with return values, implementations, and call tracking
  • Module mocking for imports with full and partial mock support
  • Spy on methods without replacing the original implementation
  • Timer mocking for debounce, setTimeout, and Date-dependent code
  • MSW (Mock Service Worker) for realistic HTTP request interception
  • Prisma client mocking with vitest-mock-extended
  • Next.js module mocks for navigation, headers, and cache

Usage#

Via CLI#

Loading code block...

Via AI Assistant#

Ask your AI assistant:

  • "Use the mocking pattern from Bootspring"
  • "Apply the Bootspring mocking pattern to my project"

Key Considerations#

  • Mock at the boundary by replacing external services, not internal logic
  • Clear mocks between tests with vi.clearAllMocks() to prevent state leakage
  • Use MSW for HTTP mocking as it is more realistic than mocking fetch directly
  • Prefer spying over mocking when you only need to observe behavior
  • Keep mock implementations simple and clearly document what they return