【问题标题】:How to avoid timeout for mocha asynchronous tests [duplicate]如何避免摩卡异步测试超时[重复]
【发布时间】:2018-02-03 17:32:53
【问题描述】:

我正在尝试运行 mocha 测试,但它不断给我以下错误

错误:超过 2000 毫秒的超时。对于异步测试和钩子,确保调用了“done()”

it('should login into account', (done) => {
        let user_login = require("../../data/login.json");
        mongoManager.insertDocuments("user", user_login.content, () => {
            loginPage.setUserName('demodgsdg');
            loginPage.setPassword('123');
            loginPage.submit();
            browser.waitForAngularEnabled(true);
            Assert.equal(element(by.id('navbar')).isDisplayed(), true, "login page is not loaded");
            setTimeout(done(), 50000);
            done();
        });

    });

在 mocha 中运行异步测试以使其不超过规定时间的最佳方法是什么?或者我应该在测试功能上设置超时时间

【问题讨论】:

  • setTimeout(done(), 50000) 到底应该完成什么?这将在 50 秒后调用 done() 的返回值。为什么?如果您在 50 秒后调用 done(代码将显示为 setTimeout(done, 50000),如果您正在这样做的话),那会有些有意义,但调用 返回值done() 的 /i> 完全没有意义。
  • Umm setTimeout(done(), 50000) and just done();基本上是解决上述问题的两种不同尝试。即使没有“setTimeout(done(), 50000);”,超时事件仍然会发生线。因此,为了简单起见,请阅读代码,假设 setTimeout 行不存在。

标签: selenium protractor timeout mocha.js assert


【解决方案1】:

你需要这样做

it('should login into account', function (done) {
        this.timeout(50000);

        let user_login = require("../../data/login.json");
        mongoManager.insertDocuments("user", user_login.content, () => {
            loginPage.setUserName('demodgsdg');
            loginPage.setPassword('123');
            loginPage.submit();
            browser.waitForAngularEnabled(true);
            Assert.equal(element(by.id('navbar')).isDisplayed(), true, "login page is not loaded");
            setTimeout(done(), 50000);
            done();
        });
});

如果您阅读https://mochajs.org/#timeouts

不鼓励将箭头函数(“lambdas”)传递给 Mocha。由于 this 的词法绑定,这些函数无法访问 Mocha 上下文。例如,由于 lambdas 的性质,以下代码将失败:

describe('my suite', () => {
  it('my test', () => {
    // should set the timeout of this test to 1000 ms; instead will fail
    this.timeout(1000);
    assert.ok(true);
  });
});

如果你不需要使用 Mocha 的上下文,lambdas 应该可以工作。但是,如果最终需要,结果将更难以重构。

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 2018-05-24
    • 2019-06-13
    • 2017-12-04
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多