I am using https://www.npmjs.com/package/axios-mock-adapter to fetch mock response .but I am not able to get mock response.
here is my code https://codesandbox.io/s/frosty-surf-g9tezx?file=/src/App.js
here I am fetching actual data
React.useEffect(() => {
const a = async function () {
const ab = await axios.get("https://jsonplaceholder.typicode.com/todos/1");
console.log(ab);
};
a();
}, []);
using axios adaptor I mock API
// This sets the mock adapter on the default instance
var mock = new MockAdapter(axios);
// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet("https://jsonplaceholder.typicode.com/todos/1").reply(200, {
users: [{ id: 1, name: "John Smith" }],
});
I need instead of actual request it will get mock response or in other words I can toggle my option sometimes it goes to actual request or some time it goes from mock api
You seem to be missing something like
import "./mockAPI"
. Right now, nothing imports your mock setup script so it never runs.I would set it up like this where you create an Axios instance for your app to use and conditionally apply the mock adapter based on an environment variable
With a
.env
file likeThen import the Axios instance from
api.js
instead of usingaxios
directly.You can have different
.env
files for different configurations