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?
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.
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.
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.
When it comes down to testing electron apps, I like:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With