eugef / node-mocks-http

Mock 'http' objects for testing Express routing functions
Other
747 stars 131 forks source link

`res.send()` doesn't convert to string #239

Closed quoininc-huudatran closed 2 years ago

quoininc-huudatran commented 3 years ago

When calling the following in my code:

res.status(200).type('application/hal+json').send({some: 'object'});

will break the code:

res._getJSONData()

as the data is kept as JSON instead of converting it to string. This is the behavior in ExpressJS (I'm not calling JSON.stringify() in my long-time running code.

restuwahyu13 commented 3 years ago

same issue with me

eugef commented 3 years ago

Are you using express or some other framework?

quoininc-huudatran commented 3 years ago

@eugef expressJS, as specified in my initial report.

This is the behavior in ExpressJS

eugef commented 3 years ago

It is indeed an issue in this lib that res.send() functionality doesn't match Express.js 100% https://github.com/howardabrams/node-mocks-http/blob/master/test/lib/mockResponse.spec.js#L639

Unfortunately the fix would break the backwards compatibility and would require a major release.

Meanwhile, as a work-around i suggest to use res._getData() in your tests.

quoininc-huudatran commented 3 years ago

@eugef Yes, I understand this, and that's what I'm using in my tests to work around, but wanted to raise/document that difference, because it took me a little time to figure it out. Maybe it can just be added to the documentation.

restuwahyu13 commented 3 years ago

@quoininc-huudatran use this for handle res.send, and this work for me res._getData()

github-actions[bot] commented 2 years ago

Stale issue message