0

I am trying to write tests for electron using spectron.

This is my code.

describe ('Application launch', function(done) {
  this.timeout(30000);

  const app = new Application({
    path: electronBinary,
    args: [baseDir],
  });

  before(() => app.start());
  after(() => app.stop());

it('shows an initial window', async () =>  {
  await app.client.waitUntilWindowLoaded();
  const count = await app.client.getwindowcount();
  assert.equal(count,1);

  });

});

However, When I run npm test The error I get is

  1) Application launch "before all" hook:
     Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.


  2) Application launch "after all" hook:
     Error: Application not running
      at Application.stop (node_modules\spectron\lib\application.js:58:48)
      at Context.after (test\spec.js:19:19)

Do I need to add any functions to the existing hooks?

Emjey
  • 1,802
  • 1
  • 12
  • 30

2 Answers2

0

You didn't use "done" as a callback in your it-function. You also don't have to use done in the describe callback. Also, since done() already makes your code asynchronous, you don't have to use the async keyword. My solution:

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

  const app = new Application({
    path: electronBinary,
    args: [baseDir],
  });

  before(() => app.start());
  after(() => app.stop());

it('shows an initial window', (done) =>  {
  await app.client.waitUntilWindowLoaded();
  const count = app.client.getwindowcount();
  assert.equal(count,1);
  done();
  });

});

Hope it helps!

  • Oh yeah didn't see that one. – Joris Pedro Valkenhoff Jan 21 '19 at 13:13
  • @JorisPedroValkenhoff thanks but I still not able to have it launched and it still would throw me the same error. I am not able to understand the done and async context either. Or is it any memory issue? – Emjey Jan 21 '19 at 13:30
  • It shouldn't be necessary as the test function is async and is returning a promise. You can either do the handler pattern like you have there or an async function and return a promise, either will work. – justin.m.chase Jan 21 '19 at 17:58
0

It appears to be happening in your before all method. Notice that your error says 1) Application launch "before all" hook:.

So your actual test function looks fine to me. And I don't see a beforeAll anywhere in this example code so I would say there is one of two possible problems.

  1. There is a beforeAll method with an issue somewhere not in this code sample.
  2. The before hook shown here is returning a non-promise object.

In your code you are using a lambda function to do the work of before but if app.start() is returning an object which is not a promise then that would be your issue. Try refactoring it more like this:

before(() => {
  app.start()
})

If your app.start() function is asynchronous you may need to pass the done handler into it:

before((done) => {
  app.start(done)
})

Or possibly convert your app.start() function to return a promise which should resolve it. You may need to add async () => app.start() but I don't think that would be necessary for a single expression like this.

justin.m.chase
  • 11,241
  • 6
  • 42
  • 84