我尝试与Mocha进行整合和单元测试,但是Mocha不会等待任何回调(或者在我的情况下,承诺履行)...
在我尝试测试的那一刻,承诺在第一次集成测试完成之前就已经完成,另一刻它崩溃......
server.js
const server = require('./app');
server.listen(process.env.PORT, () => {
console.log('Starting server.');
console.log('Displaying server information:');
console.log(`Host: http://localhost:${process.env.PORT}`);
});
app.js
require('babel/register');
/**
* Make some commonly used directories known to the process env
*/
process.env.ROOT_DIR = __dirname;
/**
* Setup Express server and the Express Router
*/
const express = require('express');
const bodyParser = require('body-parser');
const server = express();
// Parses the body for JSON object in the middleware so extra steps for
// parsing aren't required.
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({extended: true}));
server.use((req, res, next) => {
console.log('url called', req.originalUrl);
next();
});
// This is the call which has the database set up in it
// This has a few generator functions in 'm
require('./init')(server);
/**
* Export
*/
// Define routes on the server.
module.exports = server;
集成测试(这是第一个被解雇的)
import should from 'should';
import supertest from 'supertest';
import server from '../../app';
import co from 'co';
let request = supertest(server); // Set request to context of the app.
describe('Index', function () {
describe('GET /', function () {
it('returns 200', co.wrap(function * (done) {
const res = yield request
.get('/');
res.statusCode.should.equal(200);
}));
});
describe('GET /this_is_not_a_valid_route_123456', function () {
it('returns 404', function (done) {
request
.get('/this_is_not_a_valid_route_123456')
.expect(404, done);
});
});
});
命令:
NODE_ENV=test ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- ./test/ --recursive --compilers js:babel/register
有谁知道如何解决这个问题?
发生的事情的截图:
(完成初始化应该在'索引GET /'以及' Init配置回购之后......')
答案 0 :(得分:0)
你可以告诉摩卡等。
before(function (done) {
if (server.listening) return done();
server.on('listening', function() { done(); });
});
我不确定init
文件中的内容,但如果您还想等待其他活动,则可以添加多个before
个功能。