javascript - Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if retu

I am trying to write tests for electron using spectron.This is my code. describe ('Application lau

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?

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?

Share Improve this question asked Jan 21, 2019 at 12:55 EmjeyEmjey 2,0633 gold badges21 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

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 () => await app.start() but I don't think that would be necessary for a single expression like this.

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745365651a4624564.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信