我在用木偶师和小丑做一些前端测试。

我的测试如下:

describe("Profile Tab Exists and Clickable: /settings/user", () => {
    test(`Assert that you can click the profile tab`, async () => {
      await page.waitForSelector(PROFILE.TAB);
      await page.click(PROFILE.TAB);
    }, 30000);
});

有时,当我运行测试时,一切都按预期工作。其他时候,我得到一个错误:

Timeout -在jest.setTimeout指定的5000毫秒超时时间内没有调用异步回调。 在node_modules/jest-jasmine2/build/queue_runner.js:68:21 <br/> 在超时。callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:633:19)

这很奇怪,因为:

我将超时时间指定为30000 我是否得到这个错误似乎是随机的

为什么会这样?


当前回答

对于那些正在寻找解释的人 jest—runInBand,你可以去文档。

在CI环境中运行木偶师

GitHub - smooth-code/ Jest - Puppeteer:使用Jest和Puppeteer运行测试

其他回答

我想补充(这是一个有点长的评论),即使超时3000,我的测试有时仍然会(随机)失败

Timeout -在jest.setTimeout指定的5000ms超时内没有调用异步回调。

感谢Tarun的精彩回答,我认为修复大量测试的最短方法是:

describe('Puppeteer tests', () => {
  beforeEach(() => {
    jest.setTimeout(10000);
  });

  test('Best Jest test fest', async () => {
    // Blah
  });
});

当它是async from test时,它应该调用async/await。

describe("Profile Tab Exists and Clickable: /settings/user", () => {
    test(`Assert that you can click the profile tab`, async (done) => {
        await page.waitForSelector(PROFILE.TAB);
        await page.click(PROFILE.TAB);
        done();
    }, 30000);
});

我最近因为不同的原因遇到了这个问题:我正在使用jest -i同步运行一些测试,它会超时。无论出于何种原因,使用jest——runInBand(即使-i是一个别名)运行相同的测试都不会超时。

Test接受一个超时参数。见https://jestjs.io/docs/api # testname-fn-timeout。下面是一个例子:

async function wait(millis) {
  console.log(`sleeping for ${millis} milliseconds`);
  await new Promise(r => setTimeout(r, millis));
  console.log("woke up");
}
test('function', async () => {
  await wait(5000);
}, 70000);

这可能对大多数访问此页面的人没有太大帮助,但当我得到这个错误时,它与Jest无关。我的一个方法调用是在本地运行时获得一个空对象和一个空异常。一旦我添加了一个空检查,失败的测试和控制台日志就消失了。

if(response !== null){
    this.searchSubj.next(resp);
 }
 else {
    return;
 }