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

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?



Solution 1:[1]

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!

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 justin.m.chase