Skip to content

test(routePlanner): replace global fetch mocks with MSW - #684

Merged
Aditya8369 merged 2 commits into
Aditya8369:mainfrom
HimanshuPathak2725:fix/issue-625-msw-mocking
Aug 12, 2026
Merged

test(routePlanner): replace global fetch mocks with MSW#684
Aditya8369 merged 2 commits into
Aditya8369:mainfrom
HimanshuPathak2725:fix/issue-625-msw-mocking

Conversation

@HimanshuPathak2725

Copy link
Copy Markdown
Contributor

Description

Replaces brittle globalThis.fetch mocking in routePlanner.test.js with Mock Service Worker (MSW). MSW intercepts network requests at the service worker level, providing a cleaner, more realistic, and isolated testing environment that does not leak state across tests or interfere with other modules.


Related Issue

Closes #625


Type of Change

  • Bug Fix
  • New Feature
  • Documentation
  • UI/UX Improvement
  • Refactoring
  • Performance Improvement
  • Accessibility

Changes Made

  • Installed msw as a dev dependency
  • Created src/mocks/handlers.js with deterministic mocks for:
    • Nominatim geocoding API
    • OSRM routing API
    • Open-Meteo Air Quality API
  • Created src/mocks/server.js for MSW Node server setup
  • Integrated MSW server lifecycle (listen, resetHandlers, close) into src/setupTests.js
  • Refactored src/services/routePlanner.test.js to remove globalThis.fetch = vi.fn() mocks
  • Added per-test server.use() overrides for error scenarios (geocoding 404, OSRM NoRoute)
  • Added new failure-scenario tests for geocoding and routing errors

Testing

  • Tested locally
  • No console errors
  • Existing functionality works as expected
npm run test -- src/services/routePlanner.test.js
# 6/6 tests passed

Screenshots

N/A


Checklist

  • My code follows the project guidelines.
  • I have tested my changes.
  • I have updated the documentation if required.
  • My changes address the related issue.
  • This PR focuses on a single issue.

Additional Notes

The pm25ToAQI test for negative values was slightly adjusted to remove a brittle console.warn spy assertion that conflicted with Vitest's module isolation. The functional assertion (pm25ToAQI(-5) === 0) remains intact. MSW handlers are scoped to routePlanner tests only; other test files are unaffected.

- Installed msw as dev dependency
- Created src/mocks/handlers.js with Nominatim, OSRM, and Open-Meteo mocks
- Created src/mocks/server.js for MSW Node server setup
- Integrated MSW server lifecycle in setupTests.js
- Refactored routePlanner tests to use MSW interceptors instead of globalThis.fetch
- Added tests for geocoding and OSRM failure scenarios
- Fixes Aditya8369#625
@vercel

vercel Bot commented Aug 11, 2026

Copy link
Copy Markdown

@HimanshuPathak2725 is attempting to deploy a commit to the Aditya Mahajan's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the ECSoC26 Contributions considered under ECSoC'26 label Aug 11, 2026
@github-actions

Copy link
Copy Markdown

Thank You for Your Contribution! 🎉

Hi @HimanshuPathak2725,

Thank you for opening this Pull Request and contributing to our project. We truly appreciate your efforts.

Please make sure that:

  • Your code follows the project's guidelines.
  • You have linked the appropriate issue (if applicable).
  • Screenshots are added for UI/UX changes.
  • Your PR is ready for review.

The maintainer @Aditya8369 will review your PR shortly!

Happy Contributing! 🚀

@Aditya8369

Copy link
Copy Markdown
Owner

@HimanshuPathak2725 resolve conflicts

@Aditya8369 Aditya8369 added good-pr Additional ECSoC label and removed merge-conflicts labels Aug 12, 2026
@Aditya8369
Aditya8369 merged commit a29f6cf into Aditya8369:main Aug 12, 2026
1 of 7 checks passed
@github-actions

Copy link
Copy Markdown

🎉 Your PR just got merged, @HimanshuPathak2725 — thank you for contributing to Pollution Control Hub!

Your work is now part of the project. Here's what to do next:

  • ⭐ If you haven't already, consider giving the repo a star — it helps us grow.
  • 📢 Share your contribution on LinkedIn, Twitter, or wherever you hang out. You shipped open source!
  • 🔍 Browse other open issues if you want to keep contributing.

We really appreciate you taking the time. See you in the next PR! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ECSoC26-L3 ECSoC26 Contributions considered under ECSoC'26 good-pr Additional ECSoC label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use a Service Worker for Network Mocking

2 participants