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

我的测试如下:

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

为什么会这样?


当前回答

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

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= 10000 // Timeout of 10 seconds

缺省超时值为5000 (5000 ms - 5秒)。这将适用于所有测试用例。

或者如果您只想给特定的函数超时,那么您可以在声明测试用例时使用此语法。

test(name, fn, timeout)

例子

test('example', async () => {

}, 10000); // Timeout of 10 seconds (default is 5000 ms)
// In jest.setup.js
jest.setTimeout(30000)

如果在Jest <= 23:

// In jest.config.js
module.exports = {
  setupTestFrameworkScriptFile: './jest.setup.js'
}

如果在笑话> 23:

// In jest.config.js
module.exports = {
  setupFilesAfterEnv: ['./jest.setup.js']
}

随着Jest的发展,这个问题的答案也发生了变化。当前答案(2019年3月):

您可以通过向it添加第三个参数来覆盖任何单个测试的超时。也就是说,它('运行缓慢',()=>{…}, 9999) 您可以使用jest.setTimeout更改默认值。这样做: / /配置 "setupFilesAfterEnv":[//不是setupFiles . ”。/ src /嘲笑/ defaultTimeout.js” ], 而且 //文件:src/jest/defaultTimeout.js /*全局笑话*/ jest.setTimeout (1000) 就像其他人注意到的那样(与此没有直接关系),对于async/await方法来说,done是不必要的。

确保调用done();在回调中,或者它不能通过测试。

beforeAll((done /* Call it or remove it */ ) => {
  done(); // Calling it
});

它适用于所有具有done()回调的其他函数。

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

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毫秒只是默认设置。