Questions tagged [jest-fetch-mock]

99 questions
3
votes
1 answer

Using Jest even after doing fetch.mockResponse actual fetch is getting called

I am new to react and trying to writing my first test case using Jest. I have to mock a fetch response. I am using jest-fetch-mock. But the call is going to actual fetch and data returned is undefined. package.json : "jest-fetch-mock":…
Swati Joshi
  • 105
  • 3
  • 14
3
votes
0 answers

How to return an array with jest-fetch-mock?

I need to mock an fetch call which returns an array with Jest Fetch Mock: https://jsonplaceholder.typicode.com/todos/ it ('
', () => { const wrapper = render (
); fetch.mockResponseOnce ( JSON.stringify ([ { id:…
Evanss
  • 23,390
  • 94
  • 282
  • 505
2
votes
0 answers

How to mock AWS Auth.sign up/in API inside reactJs project

I'm starting with AWS-Cognito and the reactJs app, the service is working very well. I want to test this function using react-testing-library and Jest. I don't know how to mock the response Promise received from Auth.signUp(email, password)…
2
votes
0 answers

An update to MsalAuthenticationTemplate ran an effect , error when writing unit test with jest in reactjs

I am writing unit tests with jest, my unit tests are running but I am always getting below error. Due to this error, it does not show coverage and test description which ran successfully console.error Warning: An update to…
Mandeep Singh
  • 1,287
  • 14
  • 34
2
votes
0 answers

Test onClick with Jest Mock Function

I built a react app where I'm trying to get myself more confortable with testing, yet I'm quite stuck trying to test a button from my component. The documentation is very vague and I have not found any solution to my case. The onClick method is…
Fer Toasted
  • 1,274
  • 1
  • 11
  • 27
2
votes
0 answers

Fetch-Mock Error. No fallback response defined for POST

I need to test an asynchronous function (redux thunk) in jest. To do this, I decided to use fetchMock. But I'm having problems. The test ends successfully but I get an error from fetch 'fetch-mock: No fallback response defined for POST to http://…
2
votes
0 answers

jest-fetch-mock: mocks only work if they are declared in components

I'm trying to test a React app with Rails backend. I'm trying to write tests in jest. I'm using jest-fetch-mock node module to mock API calls. For some reason, it doesn't work properly. This works, but fails in tests //PhoneCodeList.jsx class…
Steve Redka
  • 108
  • 1
  • 10
2
votes
0 answers

Many issues with jest-expo -- "TypeError: Cannot read property 'fetch' of undefined"

I've searched many a thread on this issue on the site but none of them have answers or answers that work for me. I have a jest unit test suite that I'm attempting to run but keep getting Test suite failed to run -- TypeError: Cannot read property…
Chase Small
  • 321
  • 3
  • 13
2
votes
1 answer

React Native with TypeScript: How to set up globals in setup.js?

I'm building a React Native app with TypeScript. I want to use jest-fetch-mock. In the documentation it says to create setup.js file and add global.fetch = require("jest-fetch-mock"); in it. I did that. But I get the error: [ts] Cannot compile…
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
2
votes
1 answer

Click Handle on Jest

I am writing a test case using jest, but I am not able to get how to test click simulation if it is not button. If it is button we write find('button), but what if we click on div and there are nested div class Section extends React.Component { …
Shyam Kumar
  • 586
  • 3
  • 17
1
vote
0 answers

How to write test case using react testing library

const { UserData, UserSessionComplete } = useSelector((state) => state?.header); How to get this declaration under coverage.
1
vote
1 answer

"Expected signal to be an instanceof AbortSignal? with Jest Fetch Mock

I am testing an rtk query mutation api call. I'm trying to match the mock result to a successful api call but I always get an error: 'Expected signal to be an instanceof AbortSignal'. I understand that this may be a side effect of jest-fetch-mock…
Eleanor Tay
  • 39
  • 1
  • 5
1
vote
0 answers

JEST: after mocking response from API, the data is not set in component

Index.js ` import React, {useEffect} from 'react' import store from '../store_path' import loadable from '@loadable/component' import { apiCall } from './apiService_path' import useTokenHook from 'hook_path' const ChildComponent = loadable(() =>…
1
vote
1 answer

jest test case is failing for react functional component

My code is failing when i run this test. I have tried await expect(API().isFileAvailable).rejects.toEqual(Promise.reject(new Error('Bad input'))); But this doesn't work File: async (a: string, b: string | undefined, c: string | undefined) => { …
Iliyas Shaik
  • 108
  • 5
1
vote
0 answers

How to test api call using method POST with jest?

I'm trying to mock API call with library calls 'jest-when'. It works correctly When I call method GET, but it doesn't work when it's method POST. The API call that I want to mock is here const use = () => { fetch( …
Yuzu
  • 59
  • 1
  • 8