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

我的测试如下:

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配置文件中设置超时,例如:

{ // ... other stuff here
    "testTimeout": 90000
}

其他回答

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

在CI环境中运行木偶师

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

你也可以得到基于愚蠢的拼写错误的超时错误。例如,这个看似无伤大雅的错误:

describe('Something', () => {
  it('Should do something', () => {
    expect(1).toEqual(1)
  })

  it('Should do nothing', something_that_does_not_exist => {
    expect(1).toEqual(1)
  })
})

产生以下错误:

FAIL src/TestNothing.spec.js (5.427s)
  ● Something › Should do nothing

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
      
      at node_modules/jest-jasmine2/build/queue_runner.js:68:21
      at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:678:19)

虽然所发布的代码示例没有受到这种影响,但它可能是其他地方失败的原因。还要注意,我没有为任何地方设置超时——无论是在这里还是在配置中。5000毫秒只是默认设置。

对于Jest 24.9+,您还可以通过添加——testTimeout从命令行设置超时。

以下是其文档节选:

——testTimeout = <数字> 测试的默认超时(以毫秒为单位)。缺省值:5000。

将此添加到您的测试中,无需过多解释

beforeEach(() => {
  jest.useFakeTimers()
  jest.setTimeout(100000)
})

afterEach(() => {
  jest.clearAllTimers()
})

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);