OK, so I have all of these tests that depend on node-mocks-http in mocha to test my express.js controllers and they are all still working just fine.
The problem is that any new tests I create refuse to emit the 'end' event and finish. It got tot the point where I decided to write a very very simple controller function and test in a completely fresh project and I am still not getting the 'end' event to fire.
This is my controller (index.js):
exports.testFunc = function(req, res) {
res.status(200).json({
status: 'success',
status_message: 'OK',
data: {
firstRun: true
}
});
}
And this is my test (test.js):
let httpMocks = require('node-mocks-http');
let request = httpMocks.createRequest({
method: 'GET',
url: '/test',
});
let response = httpMocks.createResponse({
eventEmitter: require('events').EventEmitter
});
const loadingController = require('./index');
describe('loadingController:', () => {
describe('first_run_status: ', () => {
it('should really respond here', (done) => {
loadingController.testFunc(request, response);
response.on('end', () => {
done();
});
});
});
});
This is the exact code I have running with
./node_modules/mocha/bin/_mocha ./test.js --timeout 10000 --exit
This absolutely refuses to emit the 'end' event and end the test...
This is really driving me crazy, can anyone see what is wrong here? or what could possibly make this not work?
FYI when I log the response object to the console before running the testFunc I can see finished: false and when I log it after I can see
EventEmitter {
_headers: { 'content-type': 'application/json' },
cookies: {},
finished: true,
headersSent: true,
statusCode: 200,
....etc.
that finished: true so why would the 'end' event not be emited?