Testing ElectronJS With Mocha & Spectron

Testing ElectronJS With Mocha & Spectron

In this post I will be outlining how to test your ElectronJS Apps with spectron and mocha.

First to get started we need to install spectron.

!!!!IMPORTANT!!!!! this threw me for a while with compatibility issues go to the GitHub page and make sure you are using a spectron version compatible with your ElectronJS version.


I am using.

"devDependencies": {
    "electron": "^13.0.0",
    "mocha": "^9.1.2",
    "spectron": "^15.0.0"

If you add the packages with the correct versions you can just run.

npm install

Now in your package.json add you test run command.

"scripts": {
    "test": "NODE_ENV=test mocha"

Use the example from the GitHub page.

const Application = require('spectron').Application
const assert = require('assert')
const electronPath = require('electron') // Require Electron from the binaries included in node_modules.
const path = require('path')
//const utils = require('../utils/helpers')

describe('Application launch', function() {

    beforeEach(function() {
        this.app = new Application({
            path: electronPath,
            args: [path.join(__dirname, '..')]
        return this.app.start()

    afterEach(function() {
        if (this.app && this.app.isRunning()) {
            return this.app.stop()

    it('shows an initial window', function() {
        return this.app.client.getWindowCount().then(function(count) {
            assert.equal(count, 1)
            // Please note that getWindowCount() will return 2 if `dev tools` are opened.
            // assert.equal(count, 2)

    /*it('test image resize', async () => {
        const img = await utils.resizeImage({
            outputfolder: '/Users/samueleast/Desktop/S3AppUnitTests',
            media: '/Users/samueleast/Desktop/Doctor.png',
            height: 20,
            width: 20
        assert.equal(img, 'Success')

You can now run.

npm test

And start to test your code.

I kept getting this error.

Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called;

Make sure you are using these versions.

"devDependencies": {
    "electron": "^13.0.0",
    "mocha": "^9.1.2",
    "spectron": "^15.0.0"

It seems spectron is no longer being maintained probably worth finding another testing tool for electron. If anyone has any suggestions let me know.

Leave a comment