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

我的测试如下:

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 我是否得到这个错误似乎是随机的

为什么会这样?


当前回答

2022年3月14日,Jest 27.5文档表明了一个新流程:

https://jestjs.io/docs/api#beforeallfn-timeout

在超时前传递第二个参数测试msec的个数。作品!

test('adds 1 + 2 to equal 3', () => {
    expect(3).toBe(3);
},30000);

其他回答

2022年3月14日,Jest 27.5文档表明了一个新流程:

https://jestjs.io/docs/api#beforeallfn-timeout

在超时前传递第二个参数测试msec的个数。作品!

test('adds 1 + 2 to equal 3', () => {
    expect(3).toBe(3);
},30000);

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—runInBand,你可以去文档。

在CI环境中运行木偶师

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

对于大于27的笑话版本,您可以在规范文件的顶部添加useRealTimers。

下面是代码片段

import { shortProcess, longProcess } from '../../src/index';

jest.useRealTimers();

describe(`something`, function () {
    it('should finish fine', async function () {
        await shortProcess();
        expect(1).toBe(1);
    });

    it('should fail with a timeout', async function () {
        await longProcess();
        expect(1).toBe(1);
    });

    it('should finish fine again', async function () {
        jest.setTimeout(10 * 1000);
        await longProcess();
        expect(1).toBe(1);
    }, 10000);
});

在笑话仓库这里找到github问题。

当网络很慢或使用await进行许多网络调用时,就会出现超时问题。这些场景超过了缺省超时时间,即5000毫秒。要避免超时错误,只需增加支持超时的全局变量的超时。全局变量及其签名的列表可以在这里找到。

对于 是 24.9