Expressを使用してNode.jsサーバーを作成しています。サーバはPostgreSQLデータベースに接続します。私はテストのためにMochaを使うことを学んでいますが、テストは "beforeEach"フックでタイムアウトします。 "beforeEach"フックがいくつかのケースでは成功したが、後のケースではタイムアウトに成功したため、これは非常に奇妙です。Mochaテストで何度もPostgreSQLに接続できません
私のテストは、test/events.js
とtest/announcements.js
の2つのファイルになりました。タイムアウトエラーは、すべてのテスト(両方のファイル)を実行した場合にのみ発生します。これらを別々に実行すると、すべて正常に動作します。ここで
$ mocha --timeout 30000 "test/announcements.js"
Announcements
GET /api/announcements/list 200 84.757 ms - 175
✓ should GET all the announcements (271ms)
PUT /api/announcements/create 200 116.172 ms - 16
GET /api/announcements/list 200 18.715 ms - 238
✓ should PUT a new announcement (211ms)
GET /api/announcements/list 200 22.114 ms - 175
POST /api/announcements/edit 200 30.060 ms - 16
GET /api/announcements/list 200 9.408 ms - 153
✓ should POST an edit to existing announcement (108ms)
GET /api/announcements/list 200 22.030 ms - 175
POST /api/announcements/edit 404 13.532 ms - 49
GET /api/announcements/list 200 18.323 ms - 175
✓ should NOT POST an edit to inexisting announcement (152ms)
GET /api/announcements/list 200 27.353 ms - 175
DELETE /api/announcements/delete 200 15.780 ms - 16
GET /api/announcements/list 200 10.735 ms - 92
✓ should DELETE existing announcement (103ms)
GET /api/announcements/list 200 20.080 ms - 175
DELETE /api/announcements/delete 404 7.138 ms - 49
GET /api/announcements/list 200 5.113 ms - 175
✓ should NOT DELETE inexisting announcement (83ms)
6 passing (1s)
$ mocha --timeout 30000 "test/announcements.js"
Events
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 177.934 ms - 109
✓ should GET all the events (371ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
PUT /api/events/create 200 109.243 ms - 16
GET /api/events/list 200 10.463 ms - 144
✓ should PUT a new event (201ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 7.600 ms - 109
POST /api/events/edit 200 33.369 ms - 16
GET /api/events/list 200 5.774 ms - 97
✓ should POST an edit to existing event (89ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 10.483 ms - 109
POST /api/events/edit 404 12.057 ms - 42
GET /api/events/list 200 10.789 ms - 109
✓ should NOT POST an edit to inexisting event (99ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 10.907 ms - 109
DELETE /api/events/delete 200 16.145 ms - 16
GET /api/events/list 200 11.006 ms - 63
✓ should DELETE existing event (105ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 27.856 ms - 109
DELETE /api/events/delete 404 17.645 ms - 42
GET /api/events/list 200 16.777 ms - 109
✓ should NOT DELETE inexisting event (161ms)
6 passing (2s)
をしかし、私はすべてのテストを実行する場合:
DEBUG: "beforeEach" hook ...
行は私の
test/events.js
コード(下記)から来
$ mocha --timeout 30000 "test/announcements.js" "test/events.js"
Announcements
GET /api/announcements/list 200 84.049 ms - 175
✓ should GET all the announcements (270ms)
PUT /api/announcements/create 200 132.603 ms - 16
GET /api/announcements/list 200 15.295 ms - 238
✓ should PUT a new announcement (256ms)
GET /api/announcements/list 200 32.020 ms - 175
POST /api/announcements/edit 200 24.557 ms - 16
GET /api/announcements/list 200 7.385 ms - 153
✓ should POST an edit to existing announcement (142ms)
GET /api/announcements/list 200 21.938 ms - 175
POST /api/announcements/edit 404 14.570 ms - 49
GET /api/announcements/list 200 26.202 ms - 175
✓ should NOT POST an edit to inexisting announcement (110ms)
GET /api/announcements/list 200 34.236 ms - 175
DELETE /api/announcements/delete 200 15.140 ms - 16
GET /api/announcements/list 200 10.347 ms - 92
✓ should DELETE existing announcement (108ms)
GET /api/announcements/list 200 27.241 ms - 175
DELETE /api/announcements/delete 404 18.707 ms - 49
GET /api/announcements/list 200 5.255 ms - 175
✓ should NOT DELETE inexisting announcement (95ms)
Events
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 122.535 ms - 109
✓ should GET all the events (137ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
PUT /api/events/create 200 11.871 ms - 16
GET /api/events/list 200 17.677 ms - 144
✓ should PUT a new event (87ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 7.059 ms - 109
POST /api/events/edit 200 9.469 ms - 16
GET /api/events/list 200 11.117 ms - 97
✓ should POST an edit to existing event (70ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 14.007 ms - 109
POST /api/events/edit 404 10.613 ms - 42
GET /api/events/list 200 7.941 ms - 109
✓ should NOT POST an edit to inexisting event (82ms)
DEBUG: "beforeEach" hook started
1) "before each" hook for "should DELETE existing event"
10 passing (32s)
1 failing
1) Events "before each" hook for "should DELETE existing event":
Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
。失敗した "beforeEach"フックでは、行DEBUG: "beforeEach" hook started
だけを印刷しますが、行... hook connected
は印刷しないことに気付きました。 Postgresデータベースへのこれらの接続に何か問題がありますか?
私はPostgreSQLのログファイルをチェックアウトし、このタイムアウトエラーに気づいたが、ログに次の行を発生しますので、
LOG: could not receive data from client: Connection reset by peer
、何が起こっています!
let chai = require('chai');
let chaiHttp = require('chai-http');
var pg = require('pg');
let server = require('../app.js');
let should = chai.should();
chai.use(chaiHttp);
describe('Events',() => {
beforeEach((done) => { // Create some data to the database
try {
console.log('DEBUG: "beforeEach" hook started');
pg.connect(process.env.DATABASE_URL, function(err, client) {
console.log('DEBUG: "beforeEach" hook connected');
if (err) {
console.log(err);
done(err);
} else {
client.query('DELETE FROM events', function(err, result) {
console.log('DEBUG: "beforeEach" hook queried 1');
if (err) {
console.log(err);
done(err);
} else {
client.query(' \
INSERT INTO events (title) VALUES \
(\'Fantastic title!\'), \
(\'Another fantastic title!\')', function(err, result) {
console.log('DEBUG: "beforeEach" hook queried 2');
if (err) {
console.log(err);
done(err);
} else {
done();
}
});
}
});
}
});
} catch (err) {
console.log(err);
}
});
it('should GET all the events', (done) => {
chai.request(server)
.get('/api/events/list')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.success.should.equal(true);
res.body.data.should.be.a('array');
res.body.data.length.should.be.eql(2);
res.body.data[0].should.be.an('object');
res.body.data[0].title.should.equal('Fantastic title!');
res.body.data[1].should.be.an('object');
res.body.data[1].title.should.equal('Another fantastic title!');
done();
});
});
// There are 5 more very similar it('should ...') cases.
// I think they aren't important but tell me if I need to include them too.
});
ありがとうございました! 'doneConnect'パラメータを' pg.connect'のコールバックに追加し、 'done();'が置かれた場所に 'doneConnect();'を追加します。メリークリスマス! – TuomasK
ああクール、歓迎サー:) – Codesingh