Electron app opening multiple windows or processes in spectron test

My electron app works as expected but it keeps on opening new windows when I run spectron test that tests for number of windows opened.

Electron version – v8.0.2 , “spectron”: “^10.0.1” . I am not sure how to check exact version of spectron.
I am just running a small demo, below I will give code snippet

Note: I am running the spectron test pointing to .exe file generated by electron-packager.

Does Anyone have an idea what is the issue if possible how to solve it?

main.js

const { app, BrowserWindow } = require('electron')

function createWindow () {

  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  win.loadFile('index.html')
}

app.whenReady().then(createWindow)

index.js

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

test.js

const assert = require('assert');
const path = require('path');
const Application = require('spectron').Application;
const electronPath = require('electron');

const app = new Application({
    path: 'C:/demoElectronApp/winx64/demoelectronapp-win32-x64/demoelectronapp.exe',
  });

  describe('client_settings_app', function () {
    this.timeout(10000);

    beforeEach(() => {
      return app.start();
    });

    it('shows only one initial window', async () => {
      const count = await app.client.getWindowCount();
      return assert.equal(count, 1);
    });
  })

package.json

{
  "name": "demoelectronapp",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "electronpackage": "electron-packager . --out winx64"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "path": "^0.12.7"
  },
  "devDependencies": {
   "electron": "^8.2.2",
    "electron-packager": "^14.2.1",
    "assert": "^2.0.0",
    "mocha": "^7.1.1",
    "spectron": "^10.0.1"
  }
}

49 thoughts on “Electron app opening multiple windows or processes in spectron test”

Leave a Comment