Nock intercepts the request, but returns an empty object

I am using mocha as a testing framework, and I am trying to mock a DELETE request that uses fetch with an endpoint that returns an HTTP 204 status code.

Here is the test code:

 it('should logout user', (done) => { nock(<domain>) .log(console.log) .delete(path) .reply(204, { status: 204, message: 'This is a mocked response', }); api.logout(token) .then((response) => { console.log('IS DONE?--->', nock.isDone()); console.log('RESPONSE--->', response); done(); }) .catch((error) => { console.log('ERROR--->', error); }); }); 

This returns the following output:

 matching <domain> to DELETE <domain>/<path>: true (the above line being generated by the .log method in nock) IS DONE?---> true RESPONSE---> {} 

As you can see, the request was correctly intercepted, as indicated by the log() and isDone() nock methods, however, the returned response object is an empty object, so it is impossible to make statements about the returned HTTP status code (in this example, 204 )

Any idea what I can skip here? Why reply() method return an empty object?

UPDATE

Here is the code for the logout method, the remove method is a wrapper for the fetch request using the HTTP DELETE method.

 logout(token) { return remove( this.host, END_POINTS.DELETE_TOKEN, { pathParams: { token }, }, { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, }, ); } 
+5
source share
1 answer

I think 204 should not have a body response, so you might need to change it to 200. The server, of course, could return the answer, but I think the selection will not handle it. Another package, for example request , will process the body with a status of 204, but this request package is intended only for the server side.

Also not sure what your wrapper does, but I think you need to get the answer using response.json (). Mocha can also automatically process promises, you can simply return them. See the full example below:

 const nock = require('nock') const fetch = require('isomorphic-fetch'); const request = require('request') const domain = "http://domain.com"; const path = '/some-path'; const token = 'some-token'; const api = { logout: (token) => { return fetch(domain + path, { method: 'DELETE', headers: { 'Content-Type': 'application/json' } }); } } describe('something', () => { it('should logout user with 204 response using request package', (done) => { nock(domain) .log(console.log) .delete(path) .reply(204, { status: 204, message: 'This is a mocked response', }); request.delete(domain + path, function(err, res) { console.log(res.body); done(err); }) }); it('should logout user', () => { nock(domain) .log(console.log) .delete(path) .reply(200, { status: 200, message: 'This is a mocked response', }); return api.logout(token) .then((response) => { console.log('IS DONE?--->', nock.isDone()); return response.json(); }) .then(function(body) { console.log('BODY', body); }) .catch((error) => { console.log('ERROR--->', error); }); }); }); 

This will output:

  something matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true {"status":204,"message":"This is a mocked response"} ✓ should logout user with 204 response matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true IS DONE?---> true BODY { status: 200, message: 'This is a mocked response' } ✓ should logout user 

Used divisions:

  "dependencies": { "isomorphic-fetch": "^2.2.1", "mocha": "^3.2.0", "nock": "^9.0.6", "request": "^2.79.0" } 

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1263729/


All Articles