Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron: mocking out network requests for tests

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?

like image 911
Scott Rippey Avatar asked Aug 20 '18 16:08

Scott Rippey


People also ask

What is required if we need to use mock data for a test?

So in order to use mock data effectively, it is essential to have a good understanding of the software under test and more importantly how it uses its data. To start using mock data the software under test needs to be “tricked” into replacing real data with fake data.

What is mocked test?

Mock Testing provides you the ability to isolate and test your code without any interference of the dependencies and other variables like network issues and traffic fluctuations. In simple words, in mock testing, we replace the dependent objects with mock objects.

How do you mock in node JS?

In Jest, Node. js modules are automatically mocked in your tests when you place the mock files in a __mocks__ folder that's next to the node_modules folder. For example, if you a file called __mock__/fs. js , then every time the fs module is called in your test, Jest will automatically use the mocks.


Video Answer


1 Answers

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

like image 149
Gregor Biswanger Avatar answered Nov 15 '22 07:11

Gregor Biswanger