如何避免 mocha 异步测试超时
How to avoid timeout for mocha asynchronous tests
我正在尝试进行 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 中进行异步测试以使其不超过其分配时间的最佳方法是什么?或者我应该在测试函数上设置超时
你需要这样做
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
Passing arrow functions (“lambdas”) to Mocha is discouraged. Due to the lexical binding of this, such functions are unable to access the Mocha context. For example, the following code will fail due to the nature of 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);
});
});
If you do not need to use Mocha’s context, lambdas should work. However, the result will be more difficult to refactor if the need eventually arises.
我正在尝试进行 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 中进行异步测试以使其不超过其分配时间的最佳方法是什么?或者我应该在测试函数上设置超时
你需要这样做
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
Passing arrow functions (“lambdas”) to Mocha is discouraged. Due to the lexical binding of this, such functions are unable to access the Mocha context. For example, the following code will fail due to the nature of 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);
});
});
If you do not need to use Mocha’s context, lambdas should work. However, the result will be more difficult to refactor if the need eventually arises.