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.
npm install --save-dev electron-mocha chai
Note: With
npm
, you can install multiple dependencies at once!
Create the following folders:
|-- tests
|-- main
|-- renderer
- Create a file under
./tests/main
namedmain.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;
} );
} );
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",
npm run test:main
- Create a file under
./tests/renderer
namedapp.spec.js
- Add mocha test code to
app.spec.js
- Run both Main and Renderer tests:
npm test
npm test
runs bothtest:main
andtest:renderer