异步回调和超时未按预期工作

时间:2019-03-04 11:15:42

标签: javascript jasmine jestjs puppeteer

我不确定为什么会出现此错误。我调用GET my_index/_search { "query": { "nested": { "path": "user", "query": { "bool": { "must": [ { "match": { "user.first": "Alice" }}, { "match": { "user.last": "White" }}, { "match": { "user.first": "John" }}, { "match": { "user.last": "Smith" }} ] } } } } } 函数并定义done()。为什么会引发此错误。

  

超时-jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时时间内未调用异步回调。

jasmine.DEFAULT_TIMEOUT_INTERVAL

那什么都不做

describe('Puppeteer', () => {
    let originalTimeout;

    beforeEach(function () {
        originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
    });

  it('Logs in, redirects and does something', async (done) => {
    const browser = await puppeteer.launch({
      headless: true,
      args: [
        '--incognito'
      ]
    });
    const page = await browser.newPage();
    await page.goto('localhost:3000/login');
    ... // Login Credentials
    await page.waitForNavigation({ waitUntil: 'load' }); // redirects
    ... // perform some action on website
    expect(a < b)
      .toEqual(true);
    browser.close();
    done();
  });

    afterEach(function () {
        jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
    });
});  

以这种方式写是可行的,但是为什么呢?

describe('...', () => {
  it('...', async (done) => {
    ....
  }, 10000);
});

1 个答案:

答案 0 :(得分:0)

  • done用于回调。
  • async用于承诺。

看看他们的例子同时没有两个。

enter image description here

doneasync不能同时工作。如果您使用的是done,请删除async

相关问题