0
我正在爲使用MongoDB和Elasticsearch的後端編寫測試。問題是沒有用setTimeout
進行包裝測試失敗,並且它看起來像elasticsearch不能在測試之前將模擬數據索引到db中。下面是代碼:使用elasticsearch進行後端測試失敗,無setTimeout
let elasticSearch = require('elasticsearch');
let elasticClient = new elasticSearch.Client({
host: 'localhost:9200'
});
let app = require('./dist/app'); //path to my application
let supertest = require('supertest');
before((done) => {
elasticClient.index(elasticMockData, function() {
done();
});
});
beforeEach(() => {
request = supertest(app);
});
it('should get data from elastic',() => {
setTimeout(() => { // if I comment this timeout, test will fail
request.get('/api/elastic')
.end((err, res) => {
expect(res.body.hits.hits.length).not.to.equal(0);
})
}, 1000); // if I comment this timeout, test will fail
});
我想你會同意,暫停不是優雅,很好的解決方案,這會降低每一個測試,以1秒以上。也許,我錯過了什麼?