I'm using mocha as the testing framework and I'm trying to mock up a DELETE
request that uses fetch against an end point that returns HTTP status code 204
.
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 is being properly intercepted as stated by the log()
and isDone()
nock methods, however the response
object returned is an empty object, so it's not possible to make assertions about the returned HTTP status code (in this example 204
)
Any idea what I might be missing here?, why does the reply()
method return an empty object?
UPDATE
Here is the code for the logout
method, the remove
method is a wrapper for a fetch
request using the DELETE
HTTP method.
logout(token) {
return remove(
this.host,
END_POINTS.DELETE_TOKEN,
{
pathParams: { token },
},
{
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
);
}