首页 > 解决方案 > 异步回调和超时未按预期工作

问题描述

我不确定为什么会收到此错误。我调用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);
});

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

describe('Puppeteer', () => {
  it('Logs in, redirects and does something', () => {
    (async () => {
      const browser = await puppeteer.launch({
        headless: true,
        args: [
          '--incognito'
        ]
      });
      ....
      expect(a < b)
        .toEqual(true);
      browser.close();
    })();
  });
});  

标签: javascriptjasminejestjspuppeteer

解决方案


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

看看他们的例子怎么没有两个。

在此处输入图像描述

done并且async不会同时工作。done如果您正在使用,请删除async.


推荐阅读