How can I make nock respond with an 'undefined' body?

1k Views Asked by At

I'm testing a request handler that deals with different situations and one of them expects a response with HTTP code 204 and no response body (typeof response.body === 'undefined').

I tried to set the reply to null, undefined and not setting it at all. In all these cases, the response body is an empty string ('').

Is there a way to delete the response body from Nock?

This is the test I'm trying to run:

it('should return true if body is undefined (usually, 204 status responses - No content)', async () => {
  nock('http://test.com')
    .put('/')
    .reply(204, undefined)
//  .reply(204, null) also fails
//  .reply(204) also fails

 const result = await Request._request('http://test.com', { method: 'PUT' })

 expect(result).to.be.true
})

And this is the code I'm trying to test:

static async _request (url, options) {
    return new Promise((resolve, reject) => {
      request(url, options)
        .then((response) => {
          if (response.statusCode < 300) {
            if (typeof response.body === 'undefined') {
              resolve(true)
            } else if (isJSON(response.body)) {
              resolve(JSON.parse(response.body.replace(/\s/g, ' ')))
            } else {
              .
              .
              .
0

There are 0 best solutions below