Electron: mocking out network requests for tests

1.3k Views Asked by At

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?

2

There are 2 best solutions below

0
On

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.

1
On