Skip to content

Exercise 7: Tests

David Neal edited this page Aug 1, 2017 · 1 revision

In this exercise, we will use electron-mocha to set up tests for code used for the main Process, and code used for the Renderer. electron-mocha provides access to all the Electron libraries, as well as the DOM as part of the Renderer process.

Install electron-mocha and chai

npm install --save-dev electron-mocha chai

Note: With npm, you can install multiple dependencies at once!

Add folders for tests

Create the following folders:

|-- tests
  |-- main
  |-- renderer

Add a main.spec.js file

  • Create a file under ./tests/main named main.spec.js
  • Add the following test code to the file:
const chai = require( "chai" );
chai.should();
const electron = require( "electron" );
const app = electron.app;

describe( "main tests", () => {
    it( "should test app.getVersion()", () => {
        const version = app.getVersion();
        version.should.be.a( "string" );
        version.should.not.be.empty;
    } );
} );

Add test scripts to package.json

Update package.json with the following scripts:

    "test": "npm run test:main && npm run test:renderer",
    "test:main": "electron-mocha ./tests/main/*.spec.js --reporter spec --colors",
    "test:renderer": "electron-mocha ./tests/renderer/*.spec.js --renderer --reporter spec --colors",

Run main tests from the command line

npm run test:main

Bonus: Renderer Tests

  • Create a file under ./tests/renderer named app.spec.js
  • Add mocha test code to app.spec.js
  • Run both Main and Renderer tests:
npm test

npm test runs both test:main and test:renderer