6

I'd like to write unit tests for some of my network logic in an Electron app, and need to mock out the network endpoints.

In the past, I've used nock for mocking out HTTP requests in Node. However, my Electron code uses the electron.net module for requests (instead of Node's http module), and Nock doesn't intercept these requests.

What is the best way to mock Electron network requests?

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85

2 Answers2

0

You can easily mock your API with Postman: https://learning.postman.com/docs/postman/mock-servers/setting-up-mock/

Gregor Biswanger
  • 529
  • 3
  • 16
  • Unless you pay for premium Postman services, there are rate limits, including a **max of 1000 requests per month**. Eventually you will receive a `429` stating: `Your team plan allows 1000 mock server calls per month. Contact your team Admin to up your limit.` – ecoe Jun 24 '21 at 00:26
0

I highly recommend this simple, platform independent, and open source tool for local testing:

https://www.electronjs.org/apps/local-mock-server

This is much simpler than paid-for options like postman, but I'm guessing the author would be very amenable to enhancements if you propose a PR here:

https://github.com/jayakumarreddy/Local-Mock-Server/

The configuration simply lives in your home directory (~/.config/local-mock-server/mocks_folder/ on Linux), so what I do is simply check these mocks into my git repo and symlink them there, that way everything I do is repeatable for someone else. Just double click the AppImage (on Linux) to enable the mock and leave it minimized for as long as needed. Done.

ecoe
  • 4,994
  • 7
  • 54
  • 72