调用完成调用后,我的 mocha 测试似乎并没有停止。我有点难过,因为它看起来与我在网上找到的所有内容几乎一模一样。
这是整个 test.js 文件。
var request = require('supertest');
var app = require('../app.js');
describe('GET /', function() {
it('Should be status code 200', function(done) {
request(app).get('/').expect(200, done);
});
});
这是我的 app.js
// set variables for environment
var express = require('express');
var app = express();
var path = require('path');
// Set port
app.set('port', (process.env.PORT || 4000));
// Views as directory for all template files
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade'); // use either jade or ejs
// Instruct express to server up static assets
app.use(express.static('public'));
// Set routes
app.get('/', function(req, res) {
res.render('index');
});
// Main
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
module.exports = app;
我得到了这个输出
Node app is running on port 4000 GET / ✓ Should be status code 200 (141ms)
1) "after all" hook for "Should be status code 200"
1 passing (2s) 1 failing
1) "after all" hook for "Should be status code 200": Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test
.
最佳答案
你不应该从 app.js 调用 listen
。这就是 supertest API 变得困惑的地方。删除:
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
并坚持在一个单独的模块中。 app.js
应该只导出 app
以便您可以对其进行测试。
关于node.js - Mocha 测试超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32408141/