Unable to mock requests when testing API end point
Closed this issue ยท 2 comments
First of all, thank you for this awesome lib ๐ .
When I test my api end point, I'm doing a black box testing, this means that I don't have the access to the code it self and all the tests are creating http requests using supertest
package.
This lib expecting to "break" down the code so it will first "register" the promise, and then you will able to call mockResponse
otherwise it will throw No request to respond to!
.
The problem is that it I can't "break" down the async call.
It would be great to expose an "on.get" method which just registers the response and when the call comes resolves them with the registered response.
WDYT?
Hi, thank you for your interest int his library! The problem you refer to has been mentioned here several times before and I recognize that this is something, which can be useful for certain situations.
Unfortunately, at the moment the library is built with a queue based system at it's heart, so changing this to support responses before requests would mean a significant change in this. I'm still not sure, if this is something the library should aim for.
I hope to be able to give a solution for this problem, but don't expect to get this in the near future.
If it is based on a queue, you can push a "waiting response" for a specific url to the queue, and when you get a "real" request to the same url & you a have a waiting return it.
Finally I've managed to define a mock adapter for all axios requests, which allows me define responses before hand.
// __mocks__/axios/index.js
const axios = require('axios');
const AxiosMockAdapter = require('axios-mock-adapter');
axios.mockAdapter = new AxiosMockAdapter(axios, { onNoMatch: 'throwException' });
module.exports = axios;
which exposes mockAdapter
on global axios.