大家好,感谢您的关注。
我正在尝试使用mocha和supertest运行测试,即使工作正常,测试也会永远运行。为了避免这种情况,我在after()方法中添加了一个“process.exit(0)”,因此它可以正确构建,但对于“隔离”来说这似乎是错误的(此外,它看起来很糟糕:-))
我的package.json:
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "nodejs ./bin/www",
"test": "mocha"
},
"dependencies": {
"body-parser": "~1.0.0",
"cookie-parser": "~1.0.1",
"debug": "~0.7.4",
"ejs": "~0.8.5",
"express": "~4.0.0",
"is-my-json-valid": "^2.16.1",
"knex": "^0.13.0",
"morgan": "~1.0.0",
"mysql": "^2.14.1",
"static-favicon": "~1.0.0"
},
"devDependencies": {
"mocha": "^4.0.0",
"supertest": "^3.0.0",
"chai": "^4.1.2",
"util": "^0.10.3"
}
}
我的测试:
var request = require('supertest');
var util = require('util');
describe('Endpoints', function () {
var server;
beforeEach(function() {
server = require('../app').listen(3000);
});
afterEach(function(done) {
server.close(done);
});
after(function(done){
process.exit(0); // <-- THIS LOOKS REALLY BAD
});
it('Success ', function(done) {
var deputy = {name:"Meu Nome", uf:"SP", site_id:"1", secondary_site_id:"2"}
request(server)
.post('/api/deputy')
.send(deputy)
.expect(200, done);
});
it('Bad request ', function(done) {
var deputy = {naonome:"Nao deve funcionar"}
request(server)
.post('/api/deputy')
.send(deputy)
.expect(400, done);
});
});
为什么测试永远不会结束的任何想法?
谢谢大家
编辑:关注我的app.js,由express generator生成
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var deputy = require('./routes/deputy');
var db = require('./server/db');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/api/deputy', deputy);
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
答案 0 :(得分:3)
听起来数据库连接仍在运行。你可以用
关闭它connection.end(function(err) {
// The connection is terminated now
});
我假设您可以从/ server / db文件获取连接。如果您还没有这样做,请将其导出。
我自己不使用mysql,但这是我使用MongoDB时的后续功能:
after(function (done) {
server.close(function () {
mongoose.connection.close(done)
})
})
如果我不关闭数据库连接,测试将一直运行到最后,但永远不会完成并退出到shell。
答案 1 :(得分:0)
将--exit传递到摩卡程序。
mocha --exit