2015-09-04 256 views
1

一旦完成调用被调用,我的摩卡测试似乎并没有停止。我有点难过,因为它看起来几乎和我在网上找到的所有东西一样。摩卡测试超时

这是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

+0

你从app.js导出了什么? – beautifulcoder

回答

0

您不应该从app.js中调用listen。这就是超级API越来越混乱的地方。删除:

app.listen(app.get('port'), function() { 
    console.log('Node app is running on port', app.get('port')); 
}); 

并粘在一个单独的模块。 app.js应只导出app,以便您可以对其进行测试。

+0

仍然挂着app.listen完全删除,我看到的所有教程和其他git项目使用app.listen(请参阅:https://github.com/sahat/hackathon-starter/blob/master/app.js) –

0

我会改变你的测试。我不确定你需要你的app.js.你打电话的请求不好。您应该定义您的端点url并请求它,而不是在应用程序文件上调用请求。测试更改为这样的事情:

var request = require('supertest'); 

var url = "http://yourapi.com:4000"; 
this.timeout(15000); 

describe('GET /', function() { 
    it('Should be status code 200', function(done) { 
    request(url).get('/').expect(200, done); 
    }); 
}); 

您还可以设置this.timeout以15秒在您的要求将需要更多的时间来完成情况。至少你知道它能正常工作,但速度很慢。