如何使用 jest.setTimeout 增加超时值

How to increase the time out value using jest.setTimeout

我想验证一个可以在一分钟后更新的函数,我在我的代码中设置了一些睡眠,但我的默认超时值是 15000 毫秒,我的代码有 60000 毫秒的睡眠所以它 returns此错误:

thrown: "Exceeded timeout of 15000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value,
    if this is a long-running test."

我的代码在这里

it('shows that timeline can get updated after one minute', async () => {
    await selectTimeForTimeLine.selectTime('Last 5 minutes');
    await page.waitForTimeout(3000);
    const defaultTime = await alarmTimeLine.xAxisValues();
    await page.evaluate(() => {
      return new Promise((resolve) => setTimeout(resolve, 60000));
    });
    const correntTime = await alarmTimeLine.xAxisValues();
    expect(defaultTime).not.toEqual(correntTime);
  });

问题是我应该把“Jest.setTimeOut()”放在哪里。我想将超出的超时值增加到 70000 毫秒,以确保我的代码 运行 正常。

为单个测试设置超时,pass a third option to it/test,例如:

 it('testing balabala', async () => {
   ...
 }, 70000);

来自the jest.setTimeout() docs

Set the default timeout interval for tests and before/after hooks in milliseconds. This only affects the test file from which this function is called.

即 jest.setTimeout() 在文件级别 上处理 。他们的例子没有说清楚,但你应该在你的测试文件的顶部有 运行 jest.setTimeout():

const SECONDS = 1000;
jest.setTimeout(70 * SECONDS)

describe(`something`, () => {
  it('works', async () => {
    asset(true).isTruthy()
  });
})

更新: 我现在已经向 Jest 团队发送了一份 PR,已被接受,以澄清文档。他们现在阅读:

To set timeout intervals on different tests in the same file, use the timeout option on each individual test.