我目前使用摩卡2.5.3,supertest 2.0.0,knex 0.11.10,restify 4.1.1和sqlite3 3.1.4。爲什麼摩卡超時與restify和knex一起使用?
我有以下非常簡單的RESTify服務器:
const restify = require('restify');
const knex = require('knex')({
client: 'sqlite3',
connection: {
'filename': 'test.db'
}
});
const app = restify.createServer();
app.get('/', (req, res, next) => {
knex.select().from('nonexistent_table')
.then((rows) => {
return res.json(rows);
})
.catch((err) => {
return res.send('error');
});
});
module.exports = app;
下面的測試將導致測試在2000毫秒超時失敗的,而不是:
const assert = require('assert');
const supertest = require('supertest');
const app = require('./app');
describe('GET /', function() {
it('should not timeout', function (done) {
supertest(app)
.get('/')
.end(function(err, res) {
assert(false);
done();
});
});
});
如果到knex呼叫,而不是滿足被拒絕,測試失敗並且沒有超時。只有當knex呼叫被拒絕時,超時纔會出現。
有沒有人有想過什麼可能導致超時而不是適當的失敗?
編輯:我已經儘可能地調試了它,似乎摩卡時嘗試生成堆棧跟蹤發生超時。
查看我的回答[here](http://stackoverflow.com/a/39103469/893780):因爲斷言拋出一個錯誤,'done'永遠不會被調用_and_錯誤永遠不會被捕獲。 – robertklep
@robertklep謝謝,但我不確定這是正確的答案有兩個原因。首先,如果knex解析,測試正常失敗。由於我在測試中做了一個「assert(false)」,所以失敗的模式根本不應該取決於restify服務器的響應或knex的結果,但不知何故它確實!其次,如果我編寫一個不做異步http調用的mocha測試,並且只是拋出一個錯誤,那麼即使我從不調用'done()',測試也會失敗並且不會超時。這使我相信拋出錯誤/失敗斷言是摩卡測試失敗的正常方式。 – harrymonster