Why am I keep getting Error: Timeout of 2000ms exceeded from Mocha on async?
Why am I keep getting Error: Timeout of 2000ms exceeded from Mocha on async?
我正尝试在 Selenium 上开始 javascript 测试,但我卡在了开头。
MochaJS 从不等待测试结束,而是在 2 秒后抛出
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/pavel/code/gscc/test/test.js)
我的密码是
const {Builder, By, Key, until} = require('selenium-webdriver');
let assert = require('chai').assert;
describe('Test Suite', function() {
it('should do', async function(done) {
try {
let driver = new Builder().forBrowser('firefox').build();
await driver.get('http://www.google.com/ncr');
const title = await driver.getTitle();
assert.equal(title, 'Google');
} catch(err) {
console.log(err);
} finally {
await driver.quit();
}
done();
})
})
Mocha 说我的代码中有未解决的承诺,但真的有吗?
好的,在调查之后我会尝试回答我自己的问题。
首先,错误:超过 2000 毫秒的超时。 是一个常见问题,只是 - 超时,但测试没有 运行(因为他们需要更多时间 运行)。它记录在 Whosebug pretty good.
下一个问题是 Mocha 等待 promise 或 done 函数来完成测试。当我写
it('should do', async function(done) {
try {
let driver = new Builder().forBrowser('firefox').build();
await driver.get('http://www.google.com/ncr');
const title = await driver.getTitle();
assert.equal(title, 'Google');
} catch(err) {
console.log(err);
} finally {
await driver.quit();
}
done();
})
它既没有得到承诺也没有完成,因为它不适用于 async/await 机制,仅适用于标准承诺。
所以我删除了 done 并且 完全删除了 try-catch 块,现在它终于可以工作了!
最终代码为
describe('Test Suite', function() {
this.timeout(0);
before(async function() {
this.driver = await new Builder().forBrowser('firefox').build();
});
it('should do', async function() {
await this.driver.get('http://www.google.com/ncr');
const title = await this.driver.getTitle();
assert.equal(title, 'Google1121312213');
})
after(async function() {
this.driver.quit();
})
});
我正尝试在 Selenium 上开始 javascript 测试,但我卡在了开头。
MochaJS 从不等待测试结束,而是在 2 秒后抛出
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/pavel/code/gscc/test/test.js)
我的密码是
const {Builder, By, Key, until} = require('selenium-webdriver');
let assert = require('chai').assert;
describe('Test Suite', function() {
it('should do', async function(done) {
try {
let driver = new Builder().forBrowser('firefox').build();
await driver.get('http://www.google.com/ncr');
const title = await driver.getTitle();
assert.equal(title, 'Google');
} catch(err) {
console.log(err);
} finally {
await driver.quit();
}
done();
})
})
Mocha 说我的代码中有未解决的承诺,但真的有吗?
好的,在调查之后我会尝试回答我自己的问题。
首先,错误:超过 2000 毫秒的超时。 是一个常见问题,只是 - 超时,但测试没有 运行(因为他们需要更多时间 运行)。它记录在 Whosebug pretty good.
下一个问题是 Mocha 等待 promise 或 done 函数来完成测试。当我写
it('should do', async function(done) {
try {
let driver = new Builder().forBrowser('firefox').build();
await driver.get('http://www.google.com/ncr');
const title = await driver.getTitle();
assert.equal(title, 'Google');
} catch(err) {
console.log(err);
} finally {
await driver.quit();
}
done();
})
它既没有得到承诺也没有完成,因为它不适用于 async/await 机制,仅适用于标准承诺。
所以我删除了 done 并且 完全删除了 try-catch 块,现在它终于可以工作了!
最终代码为
describe('Test Suite', function() {
this.timeout(0);
before(async function() {
this.driver = await new Builder().forBrowser('firefox').build();
});
it('should do', async function() {
await this.driver.get('http://www.google.com/ncr');
const title = await this.driver.getTitle();
assert.equal(title, 'Google1121312213');
})
after(async function() {
this.driver.quit();
})
});