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}`, }, ); }