![](/img/trans.png)
[英]Mocha: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves
[英]Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves
我正在嘗試使用Spectron編寫電子測試。
這是我的代碼。
describe ('Application launch', function(done) {
this.timeout(30000);
const app = new Application({
path: electronBinary,
args: [baseDir],
});
before(() => app.start());
after(() => app.stop());
it('shows an initial window', async () => {
await app.client.waitUntilWindowLoaded();
const count = await app.client.getwindowcount();
assert.equal(count,1);
});
});
但是,當我運行npm test
,我得到的錯誤是
1) Application launch "before all" hook:
Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
2) Application launch "after all" hook:
Error: Application not running
at Application.stop (node_modules\spectron\lib\application.js:58:48)
at Context.after (test\spec.js:19:19)
我是否需要向現有的掛鈎添加任何功能?
您沒有在it函數中使用“ done”作為回調。 您也不必在describe回調中使用done。 另外,由於done()已經使您的代碼異步,因此您不必使用async關鍵字。 我的解決方案:
describe ('Application launch', function() {
this.timeout(30000);
const app = new Application({
path: electronBinary,
args: [baseDir],
});
before(() => app.start());
after(() => app.stop());
it('shows an initial window', (done) => {
await app.client.waitUntilWindowLoaded();
const count = app.client.getwindowcount();
assert.equal(count,1);
done();
});
});
希望能幫助到你!
它似乎正在您的before all
方法中發生。 請注意,您的錯誤顯示1) Application launch "before all" hook:
。
因此,您的實際測試功能對我來說還不錯。 而且在此示例代碼中的任何地方都看不到beforeAll
,因此我會說存在兩個可能的問題之一。
beforeAll
方法存在問題。 before
鈎子返回一個非承諾對象。 在您的代碼中,您使用的是lambda函數來完成之前的工作,但是如果app.start()
返回的對象不是promise,那將是您的問題。 嘗試像這樣重構它:
before(() => {
app.start()
})
如果您的app.start()
函數是異步的,則可能需要將完成的處理程序傳遞給它:
before((done) => {
app.start(done)
})
或者可能將您的app.start()
函數轉換為返回應解決的promise。 您可能需要添加async () => app.start()
但我認為這樣的單個表達式不是必需的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.