Questions tagged [jest-mock-axios]

33 questions
1
vote
1 answer

How do I use a var as the return value in a mocked Jest function?

I currently have this code... const context = {}; context.response = {}; jest.mock('axios', () => ({ defaults: { withCredentials: true }, post: () => Promise.resolve(context.response) })); When I try to run I…
JGleason
  • 3,067
  • 6
  • 20
  • 54
1
vote
1 answer

Using Jest to Mock Axios and Mock Windows.Location.Assignment

Some ReactJs component are using the Axios NPM library to fire off Http Posts. Using the post example from Axios, we have: axios.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }) .then(function (response) { …
0
votes
0 answers

Cannot use import statement outside a module (using react testing library)

During developing vite react project I got a problem with react testing library or jest. I try to test async user fetching function, but jest is throwing an error "Jest encountered an unexpected token" && "Cannot use import statement outside a…
0
votes
1 answer

Error: Uncaught [TypeError:default is not a constructor] jest mock

I have a service class FooService.js which I'm using in my component export default class FooService { saveInfo = (params) => { //method implementation here. }; } I'm using this in my component Foo.tsx in saveData method as below: const…
0
votes
1 answer

Unable to mock a user defined Axios Class in Javacript/Typescript

I am attaching all the function snippets below. I am using jest to run a unit test on this function but this needs to mock axios. I tried like this : // TODO - mock axios class instance for skipped Test suites describe("dateFilters()", () => { …
etotientz
  • 373
  • 5
  • 18
0
votes
1 answer

Mocking an api call in Jest with React Typescript

I'm following some documentation on mocking an api call with Jest, although trying to do it with react.tsx. I've looked at a lot of different stack Q&As and elsewhere online and am not understanding what I am missing from my test file to make my…
0
votes
1 answer

MockResolvedValueOnce returns undefined first and then returns the mocked value correctly

I'm trying to mock my axios, but I'm not getting it. The mock is returning undefined when I call the api, and then it returns with the right object. I can't identify the error why when calling the api for the first time it returns undefined if…
0
votes
0 answers

Testing API with axios - getting triggerUncaughtException(err, true /* fromPromise */);

function signup(data_var) { const options = { method: 'POST', url: 'xxx', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer undefined', }, data: data_var, }; return axios …
0
votes
1 answer

Why isn't Axios method running?

I have the following vue app that allows a user to login. I am running a unit test that should login but axios won't run when the submit method is triggered. The Submit method being called by button trigger in the test: methods: { submit() { var…
0
votes
1 answer

Async redux actions tests always return pending

I am writing tests for some async actions however the tests are failing because the type which is returned is always REQUEST_PENDING. So even for the tests when the data is fetched the type does not change and the test fails. I am not sure what I am…
user6248190
  • 1,233
  • 2
  • 16
  • 31
0
votes
1 answer

Axios unit test with React testing library and typescript

I am trying to test an axios API call that happens on onload of a page using react testing library. The mock Axios instance seems not firing even after passing the props into the component. I want to test both error and success scenarios. I have…
0
votes
1 answer

Jest - dontMock function not working as expected

Im trying to unmock axios module inside the test function, but it keeps returning the mocked response even tho i have declared the dontMock function. What am i doing wrong? import Axios from 'axios'; jest.mock('axios'); …
Nick
  • 2,818
  • 5
  • 42
  • 60
0
votes
1 answer

My test case for an axios method says the return type is an anonymous function

The method to be tested is const fetchCard = (id) => async (dispatch) => { const response = await axiosWrapper.get(`/giftCards/${id}`); dispatch ({ type: FETCH_CARD, payload: response }) } The test file is : import…
0
votes
1 answer

how to check function is called or not in react js?

I am trying to test my service which have one function saveWithoutSubmit export const saveWithoutSubmit = async ( values, orderId, taskId, fseMsisdn, updateTaskListAfterFilter ) => { var obj = { remarks: values.remarks, …
user944513
  • 12,247
  • 49
  • 168
  • 318
0
votes
0 answers

Nodejs Jest mocking is not working in multiple describe block in a single file, it works in first describe block only

I am using jest for unit testing in my node express application,please excuse , because i am new to all this in my abc.test.js const s3Helper = require('../../../../../lib/s3_helper'); beforeEach(async () => { s3Helper.uploadBufferToS3 =…