【发布时间】:2017-05-09 19:19:52
【问题描述】:
我正在使用 Express 创建一个 Node.js 服务器。服务器连接到 PostgreSQL 数据库。我正在学习使用 Mocha 进行测试,但现在“beforeEach”挂钩中的测试超时。这很奇怪,因为“beforeEach”钩子在某些it('should ...') 情况下成功完成,但在以后的情况下会超时。
我的测试现在位于两个文件中:test/events.js 和 test/announcements.js。仅当我运行所有测试(两个文件)时才会发生超时错误。如果我单独运行这些,一切正常。
这是我单独运行测试时得到的结果:
$ 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)
但如果我运行所有测试:
$ 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.
DEBUG: "beforeEach" hook ... 行来自我的test/events.js 代码(如下)。我注意到在失败的“beforeEach”钩子中它只打印行DEBUG: "beforeEach" hook started,而不是行... hook connected。那么这些与 Postgres 数据库的连接有问题吗?
我检查了我的 PostgreSQL 日志文件,并注意到这个超时错误导致日志中出现这一行:
LOG: could not receive data from client: Connection reset by peer
那么,发生了什么事!
这是我的test/events.js 代码:
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.
});
【问题讨论】:
标签: node.js postgresql mocha.js