大家好,感謝您的關注。摩卡測試永久運行
我正在嘗試使用摩卡和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,通過快遞發電機
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;
顯示'app.js'模塊的代碼。 – alexmac
完成,在問題中添加 –