Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to unit test components in main process of an electron app?

I have an electron app with the following folder structure:

app/js/
     |_ main.js
     |_ myClipboard.js
     |_ view/ 
         |_ render.js

Now main.js calls myClipboard to store some user data locally using nedb (document store). I want to test the logic in myClipboard since it will involve some conditional logic.

What makes this difficult is myClipboard requires the clipboard module provided by electron. The following line fails in a test setup (jasmine-node):

var clipboard=require('clipboard')

It says it couldn't find 'clipboard' module. I guess the test needs to be run from inside electron.

How do I test this?

I tried using electron-mocha, but it seems to have problems with async module that nedb uses. None of the calls like insert, update, etc work in test environment, but they work fine when I bring up my app.

Am I missing anything here?

like image 258
indraneel Avatar asked Apr 01 '16 08:04

indraneel


People also ask

How do you test an electron app?

Start recording a test by going to Test > Record > Record Keyword Test. Expand the Recording toolbar, click Run App, and then select the Electron application. Create Property Checkpoints by clicking Add Check. Checkpoints verify objects and values in the application that's being tested.

What are the three steps in a unit test?

The idea is to develop a unit test by following these 3 simple steps: Arrange – setup the testing objects and prepare the prerequisites for your test. Act – perform the actual work of the test. Assert – verify the result.

What is unit testing procedure?

Unit Testing is defined as a type of software testing where individual components of a software are tested. Unit Testing of the software product is carried out during the development of an application. An individual component may be either an individual function or a procedure.


1 Answers

When it comes down to testing electron apps, I like:

  • To unit test some isolated modules,
  • Do some end to end tests to see if the application works as a whole.

End to end tests can now be achieved fairly easily with spectron.

For unit testing though, some of our modules might be based on the electron package that is not available if we simply run the tests with node.

You can easily stub the electron package with proxyquire. Be sure to prevent calls to require original dependencies with the proxyquire @noCallThru attribute, or by requiring proxyquire like this require('proxyquire').noCallThru(). This will ensure your tests do not even try to require the orginal dependency, making it possible to run your unit tests within a simple node process.

Another solution would be that you leverage dependency injection and make sure that your module does not rely on anything introduced by the electron package.

like image 94
rayrutjes Avatar answered Oct 07 '22 12:10

rayrutjes