2015-09-04 250 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秒在您的要求將需要更多的時間來完成情況。至少你知道它能正常工作,但速度很慢。