簡體   English   中英

摩卡測試超時

[英]Mocha tests timeout

我有一個單元測試來注冊功能。 我返回一個承諾,但收到一個錯誤:

Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我試過了done()函數,異步等待,檢查Promise拒絕-沒有結果。

我的測試代碼如下:

 import authController from '../../../src/controllers/authController'; import { expect } from 'chai'; import sinon from 'sinon'; describe('Test register method', () => { it('Register method should create new user', () => { const req = { file: sinon.spy(), body: { name: "John", email: "johndoe@example.com", phone: "123", gender: "Male", birthDate: "12-12-1992", purpose: "Friends", password: "Secret" } }; const res = { status: function () { return this; }, json: sinon.spy() }; return authController.register(req,res).then(() => { expect(res.json.firstCall.lastArg.success).to.equal(true); }); }); }); 

被測控制器的代碼:

https://github.com/elszczepano/FindMates-API/blob/master/src/controllers/authController.js

我解決了! 首先:我必須將package.json中的超時設置為10000毫秒:

"test": "nyc mocha --require @babel/register --require @babel/polyfill tests/unit/**/*.test.js --timeout 10000"

第二:我必須與我的MongoDB數據庫建立連接(當然還要在測試后斷開連接)。

 before(done =>{ mongoose.connect('mongodb://localhost:27017/FindMates', {useNewUrlParser: true }); mongoose.set('useCreateIndex', true); mongoose.set('useFindAndModify', false); done(); }); after(done => { mongoose.disconnect(); done(); }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM