[英]Assertion error when testing async function with mocha and chaiAsPromised
[英]error with async function in 'before' - testing with mocha and puppeteer
我正在嘗試在“之前”掛鈎中啟動鉻瀏覽器。 運行代碼時,我不斷收到以下錯誤:
let browser;
describe('Login Tests', function(){
let page;
before(async ()=> {
let launchOptions = {
headless: false,
executablePath: '/Applications/Chromium.app/Contents/MacOS/Chromium',
args: ['--start-maximized']
};
browser = await puppeteer.launch(launchOptions);
page = await browser.newPage();
await page.goto('http://localhost:8080');
await page.waitForSelector('body div');
})
it('should bring up login modal when clicking Client Login', async function(){
await page.waitForSelector('.navbar-start a[href$="#/login"]');
await page.click('.navbar-start a[href$="#/login"]');
let url = await page.url()
assert.equal(url, 'http://localhost:8080/#/login');
})
//....
})
錯誤:超過 2000 毫秒的超時。 對於異步測試和鈎子,確保調用了“done()”; 如果返回 Promise,請確保它已解決。
當我將回調傳遞給異步 function 並在完成后返回它時,我仍然收到以下錯誤。 我在 after 鈎子中也遇到了同樣的錯誤。
有人可以幫我解決這個問題,卡在這個問題上太久了:(
問題是 mocha 每個鈎子和測試的默認超時時間為 2000 毫秒。 您似乎沒有在任何地方覆蓋默認值。
要覆蓋它,您有幾個選項,一個是將此選項添加到.mocharc.json
:
{
"timeout": 20000
}
這將為您提供 20 秒的“一切”超時 - 鈎子、測試。
另一個如何設置不同超時的選項就在您的描述回調 function 中,通過調用timeout()
方法:
describe('...', function () {
this.timeout(20000);
before(...);
it(...);
});
這將再次為您提供 20 秒的超時。 這將影響 describe 塊內的所有鈎子和測試。 如果你想只為 before 鈎子設置超時,你在 before 鈎子中輸入這個:
describe('...', function () {
before(function () {
this.timeout(20000);
});
it(...);
});
設置不同超時的另一種方法可能是:
it('...', function () {
// ...
}).timeout(20000);
這顯然只會為這個測試設置一個超時。
如果我應該給出一個偏好,我會選擇第一個選項,因為這是我期望的配置值。 鈎子和測試是關於測試步驟的,而不是設置超時,所以我會避免在那里做。 如果我需要為一個特定的測試設置不同的超時時間,出於同樣的可讀性原因,我會選擇我提到的最后一個選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.