3

I am writing unit tests for an ts node api.

I am using the chai-http to instantiate/mock requests, however using the .query option referred to in the docs here does not seem to actually add my query params to the request objects qs parameter.

const r = chai.request(server)
                .get('/connections')
                .query({
                    page: 1,
                    size: 10,
                    connection_id: id
                 })

ConnectionController.List(r, null);

Inspecting the req object, it has added them to the _header: _path: and :url but nothing under _query or qs. The underlying function that takes in the req will try to parse the value using req.query.page... req.query.size etc, but breaks due to the request query object being undefined.

r.qs = Array(0) []

but _header: , _path: and url: show

"GET /connections?page=1&size=10&connection_id=some_random_connection_id HTTP/1.1

Does anyone know why this would be the case?
Is there a different mocha request library that will provide correct assignment of query params?

I have tried manually adding them like so, which works and adds them to qs. but I would have expected .query() to add them here.

r.query.page = '1';
r.query.size = '10';
r.query.connection_id = 'random_id';
Rogan88
  • 111
  • 2
  • 14
  • Hello @Rogan88, it seems like you are trying to test variable assignment to a third party library (which is here superagent underlying chai). Indeed, nor the qs and query are assigned but it shouldn't be your concerns since query parameters are properly sent to the server. – user3793803 Nov 02 '20 at 11:11

0 Answers0