JEST:“在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调。”,尽管它已经配置

Posted

技术标签:

【中文标题】JEST:“在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调。”,尽管它已经配置【英文标题】:JEST: "Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.", despite it being already configured 【发布时间】:2018-12-05 11:36:55 【问题描述】:

在你指出之前,是的,我知道这似乎是多个问题的重复;

JEST: Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout Got Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout

但是,我实施了建议的所有 3 个修复;

在测试中使用jest.setTimeout() 设置异步超时 使用test() 的第三个参数传入一个扩展的异步超时限制 完成后调用done函数

但是,在自动 linux 机器 (Jenkins) 上运行我的 jest 测试时,它仍然抛出相同的错误。另外,值得一提的是,这在我运行 NodeJS v10 的 MacOS 机器上运行良好,而自动化的 linux 机器运行 NodeJS V8.8.3(最新的 LTS 版本)

这就是我开玩笑的测试的样子;

const webdriverio = require('webdriverio');
const options = 
    desiredCapabilities: 
        browserName: 'chrome',
        chromeOptions: 
            args: ["--no-sandbox", "disable-web-security", "--disable-dev-shm-usage"]
         
     
;
const client = webdriverio.remote(options);

beforeEach(async () => 
    await client.init();
)

test('Google Search for WebdriverIO has correct title', async (done) => 
    jest.setTimeout(30000)
    await client.url('https://www.google.com/ncr');
    await client.setValue('input[name=q]', 'WebdriverIO');
    await client.click('input[value="Google Search"]');
    const title = await client.getTitle();
    expect(title).toBe('WebdriverIO - Google Search');
    done();
, 30000);

afterEach(async () => 
    await client.end();
);

这是我尝试运行测试时得到的日志;

09:57:19 > jest --config jest.config.js
09:57:19 
09:57:20 Installing selenium server ...
09:57:22 Starting selenium server ...
09:57:23 Selenium server started ...
09:57:29 FAIL jest/test/google.spec.js (5.874s)
09:57:29   ��� Google Search for WebdriverIO has correct title (5016ms)
09:57:29 
09:57:29   ��� Google Search for WebdriverIO has correct title
09:57:29 
09:57:29     Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
09:57:29 
09:57:29       at mapper (node_modules/jest-jasmine2/build/queue_runner.js:41:52)
09:57:29 
09:57:29   ��� Google Search for WebdriverIO has correct title
09:57:29 
09:57:29     A session id is required for this command but wasn't found in the response payload
09:57:29 
09:57:29       at new RuntimeError (node_modules/webdriverio/build/lib/utils/ErrorHandler.js:143:12)
09:57:29       at RequestHandler.createOptions (node_modules/webdriverio/build/lib/utils/RequestHandler.js:121:23)
09:57:29       at RequestHandler.create (node_modules/webdriverio/build/lib/utils/RequestHandler.js:212:43)
09:57:29       at Object.url (node_modules/webdriverio/build/lib/protocol/url.js:24:32)
09:57:29       at Object.exec (node_modules/webdriverio/build/lib/helpers/safeExecute.js:28:24)
09:57:29       at Object.resolve (node_modules/webdriverio/build/lib/webdriverio.js:191:29)
09:57:29       at lastPromise.then.resolve.call.depth (node_modules/webdriverio/build/lib/webdriverio.js:486:32)
09:57:29       at _fulfilled (node_modules/q/q.js:854:54)
09:57:29       at self.promiseDispatch.done (node_modules/q/q.js:883:30)
09:57:29       at Promise.promise.promiseDispatch (node_modules/q/q.js:816:13)
09:57:29 
09:57:29 Test Suites: 1 failed, 1 total
09:57:29 Tests:       1 failed, 1 total
09:57:29 Snapshots:   0 total
09:57:29 Time:        5.988s, estimated 7s
09:57:29 Ran all test suites.
09:57:29 Killing selenium server ...

任何关于为什么在我的本地机器上正常工作时可能会失败的想法将不胜感激。另外,我尝试在我的 Jest 全局设置文件中设置 jest.setTimeout,但它会抛出 jest.setTimeout is not a function;

https://github.com/facebook/jest/issues/3788

【问题讨论】:

尝试将测试用例主体包装成一个try-catch块,最后放入done。这可能有助于超时。 如果我遇到你的情况,我会把它作为一个错误发布到github.com/facebook/jest/issues。如果不是bug,对jest有广泛了解的人会指出来的。 我从未见过异步函数与“完成”回调一起使用,我认为你应该使用其中一个。另外,您是否尝试过将超时直接设置为茉莉花,如您链接的上一期中所示jasmine.DEFAULT_TIMEOUT_INTERVAL = <your_timeout> 我在 15 天前就这个问题开了一个玩笑的问题,但还没有收到回复,github.com/facebook/jest/issues/6557 @skyboyer 你可以让 Jenkins 上的 suer NodeJS 版本和你本地机器上的一样吗?另外,请确保浏览器可以在 Jenkins 中启动! 【参考方案1】:

我的测试中有类似的东西。我的测试在本地顺利通过,但在 GitHub Actions 上失败了。

我的问题是,在本地,已经下载了测试工具(在我的例子中是内存中的 MongoDB。在你的例子中是 chrome 浏览器),但在远程环境中,它们必须先下载。

检查您的 Jenkins 日志以了解您下载 chrome 的环境,在下载达到 100% 之前测试失败。即使您没有发现,您在问题中发布的日志也暗示了这个方向,因为日志打印出超时设置为5000ms,即使您将第一次测试的超时设置为不同的价值:

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

这告诉我超时的测试部分是测试之前的部分

要解决这个问题,您需要专门为 beforeEach 函数添加更长的超时时间,在该函数中初始化 webdriver。这样,在第一次测试中,当下载 chrome 时,您将有足够的时间来完成下载 chrome。在接下来的测试中,这应该不是问题,因为 chrome 已经可用。 还建议在 Jenkins 中主动下载 chrome\chromium 浏览器,以便在测试运行开始之前就可以使用它

所以,我建议你试试这个:

const webdriverio = require('webdriverio');
const options = 
    desiredCapabilities: 
        browserName: 'chrome',
        chromeOptions: 
            args: ["--no-sandbox", "disable-web-security", "--disable-dev-shm-usage"]
         
     
;
const client = webdriverio.remote(options);

beforeEach(async () => 
    await client.init();
, 30000) // <-- This is what you need. not the timeout in the actual test

test('Google Search for WebdriverIO has correct title', async () => 
    jest.setTimeout(30000); // you can keep this if you think is needed,
                            // but I'm pretty sure it's not
    await client.url('https://www.google.com/ncr');
    await client.setValue('input[name=q]', 'WebdriverIO');
    await client.click('input[value="Google Search"]');
    const title = await client.getTitle();
    expect(title).toBe('WebdriverIO - Google Search');
);

afterEach(async () => 
    await client.end();
);

Here's an example of an old run,失败是因为内存数据库在超时之前没有完成下载。即使数据库尚未准备好使用,这也会导致测试开始。

将超时添加到my beforeEach functions 后,它就停止了。

如果您在建议的修复后仍然面临问题,请随时对此答案发表评论。如果这是正确答案,请不要忘记标记答案:-)

【讨论】:

以上是关于JEST:“在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调。”,尽管它已经配置的主要内容,如果未能解决你的问题,请参考以下文章

单元测试jest部署

jest中的mock,jest.fn()jest.spyOn()jest.mock()

jest中的mock,jest.fn()jest.spyOn()jest.mock()

Jest-vue 和 Vue-jest 有啥区别?

jest 忽略某些jest文件

如何仅通过一个 Jest 配置文件/设置使用多个 Jest 预设?