Questions tagged [axios-mock-adapter]

82 questions
1
vote
2 answers

Component making axios request being mounted twice in order for asynchronous test to pass, if it is mounted once the test fails. WHY

I am making a call to an internal server to give me back whatever data, and in order to mock this i am using axios-mock-adapter and sending back an array with 5 things. I have to mount component twice to make this test pass. here is my component: …
mlisonek
  • 180
  • 12
1
vote
1 answer

Mock-axios-adapter not mocking get request

I'm trying to test this function: export const fetchCountry = (query) => { return dispatch => { dispatch(fetchCountryPending()); return axios.get(`${process.env.REACT_APP_API_URL}/api/v1/countries/?search=${query}`) .then(response =>…
peter
  • 1,482
  • 4
  • 16
  • 38
1
vote
1 answer

Why does Axios get call return a 404 error with basic setup?

I'm trying to set up a React project and want to get a basic get call working. The url I'm using is https://jsonplaceholder.typicode.com/todos/1 just for testing and to see if everything is working well. However, I get a 404 error back. The call I…
anon311
  • 11
  • 1
  • 5
1
vote
1 answer

Mock an axios request to external service in test

I'm trying to mock a call to an external service from NodeJS express. I can't get axios-mock-adapter to intercept the actual axios call (http://api.openweathermap.org) and to return a mocked response. When the assert is done it fails because to…
Skjaar
  • 89
  • 1
  • 10
1
vote
1 answer

how to match a returned Promise in using Jest with redux action

I am using reactjs with redux and the action is using axios and returning a Promise. my test is not matching the type. I have tried different ways of doing it but end up with the same problem. also tried…
shorif2000
  • 2,582
  • 12
  • 65
  • 137
1
vote
1 answer

Why doesn't the mockAxios.post.mockImplementationOnce return any data

Why doesn't the mockAxios.post.mockImplementationOnce return any data? I would like to see the errors. it('should show errors when submitting returns a 422 response', () => { mockAxios.post.mockImplementationOnce(() => …
Joseph Chambers
  • 3,698
  • 4
  • 22
  • 37
1
vote
2 answers

axios-mock-adapter If I mock the requests then works only the requests that I mock

I'm trying to mock API that not implemented on server yet. But if I mock the requests then works only the requests that I mock. If I add mock.restore(); after mock.onGet then real API works fine, but then there is no mock API that I need too. import…
1
vote
2 answers

Use axios and axios-mock-adapter

I'm trying to use axios and axios-mock-adapter in one place to aggregate more mocks and then export the axios instance to use it wherever I want: mock.js import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; let instance =…
Piero
  • 9,173
  • 18
  • 90
  • 160
1
vote
1 answer

The reason of usage axios-mock-adapter

I'm a beginner to test axios calls and started using axios-mock-adapter but I don't get why we use axios-mock-adapter. mock.onPost('/api').reply(200, userData, headers); In this code snippet, does the request really go to the server or is this just…
Burak ULU
  • 305
  • 1
  • 6
  • 17
0
votes
0 answers

GET endpoint using the axios mock adapter not working in Vue component

Add GET endpoints using the axios mock adapter. How to I return in my Vue component: my mock api file: var axios = require("axios"); var MockAdapter = require("axios-mock-adapter"); // This sets the mock adapter on the default instance var mock =…
Pianoc
  • 763
  • 1
  • 11
  • 32
0
votes
1 answer

Getting 'Request failed with status code 404' when testing using axios-mock-adaptor

Axios instance creation in default.js file. import axios from "axios"; const BASE_URL = import.meta.env.VITE_BACKEND_BASE_URL; const instance = axios.create({ baseURL: BASE_URL, }); instance.defaults.headers.common["Accept"] =…
Suyash Mittal
  • 73
  • 1
  • 13
0
votes
0 answers

Expecting error with axios-mock-adapter only works inside a try-catch block or when only one test is run

I'm mocking an axios instance using axios-mock-adapter for unit testing a service that connects to an API. api.ts export const api = axios.create({ baseURL: `${process.env.PUBLIC_URL}/api`, }); answers.service.test.ts import { api } from…
0
votes
0 answers

How to test that a element is not in the screen while a http request is made?

I have an application using React and axios, and I want to this the following behvior: Fill a form submit form Success message shows Fill the form again submit form While the request is made, the success message should not be showing** request…
thur
  • 964
  • 1
  • 15
  • 27
0
votes
0 answers

Axios-mock-adapter giving Promise { _x: 0, _y: 0, _z: null, _A: null } when mocking the axios in react-native

I want to mock the Axios API in react-native and use axios-mock-adapter to mock the API. Below is the code. const axiosMock = new MockAdapter(axios); console.log("axiosMock ==>", axiosMock); But when I run the test the axiosMock gives me a promise…
Pathik Gandhi
  • 1,345
  • 1
  • 17
  • 36
0
votes
0 answers

React Testing, using axios-mock-adapter

I need to switch out my backend in-memory DB for testing due to memory issues. Below is my code import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import App from…
user1730289
  • 273
  • 4
  • 15