0

So I set the main.test file as the sample one provided on npm spectron site but can't get it working. A new window is launched but Promise doesn't resolve. Working with electron 13, spectron 15.

const { Application } = require('spectron')

describe('Application launch', function () {
  this.timeout(100000)

  beforeEach(function (done) {
    this.app = new Application({
      // Your electron path can be any binary
      // i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
      // But for the sake of the example we fetch it from our node_modules.
      path: electronPath,

    

      // The following line tells spectron to look and use the main.js file
      // and the package.json located 1 level above.
      args: [path.join(__dirname, '.')]
    })
    return this.app.start(done)
  })

  afterEach(async function () {
    if (this.app && this.app.isRunning()) {
      await this.app.stop()
    }
  })

  it('shows an initial window', async function (done) {
    done();
    if (this.app.client.isRunning()) {
      console.log('CLIENT IS RUNNING')
      this.app.client.getWindowCount().then(function(count) {
        // Do something with the window count
        done();
      }).catch(function(error) {
        // Handle the error
        done(error);
      });
    } else {
      done(new Error('Spectron is not connected to the Electron application'));
    }
  })
})

Error:

Application launch "before each" hook for "shows an initial window": Error: Timeout of 100000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\aw032f\Development\cme\cme-desktop_release-nightly\packages\electron-app\main.test.js)

Really appreciate some help here!

I'm trying to run successfully a test for the main.js file of my electron app, version 13,which code is mostly this one:

listener();

/**
 * Creates a window.
 */
function createWindow() {
  const win = new BrowserWindow({
    width: 1240,
    height: 680,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
  });

  win.loadFile(path.join(__dirname, 'index.html'));
  win.webContents.on('did-finish-load', () => {
    emitter(win.webContents);
  });
}

app.whenReady().then(() => {
  createWindow();

  app.on('activate', function() {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on('window-all-closed', function() {
  if (process.platform !== 'darwin') {
    poolShutdown();
    app.quit();
  }
});

0 Answers0