I have a simple service I'd like to test, by calling its controller. I thought I'd use node-mocks-http to create a mock request, and look at the results of the response. However, despite all the documentation and sample code, I could not get the response to emit any event ("end", "send" or even "error"), and can't therefore know when to test the output.
Here's a simple function (using Express 4.*):
export function getServiceHealth(req, res) {
let message = 'service has been up for ' + process.uptime() + ' seconds!';
res.status(200).send(message);
}
Here's my test (Jasmine):
import {EventEmitter} from 'events';
import httpMock from 'node-mocks-http';
import {getServiceHealth} from '../../lib/controllers/health/';
describe('Service health integration tests', () => {
it('should get health', done => {
let req = httpMock.createRequest({url: '/health'});
let res = httpMock.createResponse({EventEmitter: EventEmitter});
getServiceHealth(req, res);
res.on('end', () => {
console.log(res._getData());
done();
});
res.on('send', () => {
console.log(res._getData());
done();
});
//setTimeout(() => {console.log(res._getData()); done();}, 1000);
});
});
The only way I ever get this test to finish (without throwing a timeout error) is by uncommenting the setTimeout
line - obviously not the right way to go. And when I do, the data in res
is exactly what I expect it to be - meaning that other than event firing, everything works ok.
What do I have to do to get the event triggered on res
?
PS: on the off chance that this is some ES6 import shenanigan, I tried this:
let res = httpMock.createResponse({EventEmitter: require('events').EventEmitter});
Same result.
PPS: Opened issue on repo
Aucun commentaire:
Enregistrer un commentaire