Questions tagged [jest-fetch-mock]

99 questions
23
votes
2 answers

Jest: How to spy on an imported function

Putting in a small snippet below : import xyz from '../xyz' function calculate() { return xyz(arg1, arg2).catch((err) => { func1() func2() }) } export default calculate I'm just trying to assert that xyz is called in jest. How can I do…
user2821242
  • 1,041
  • 3
  • 9
  • 16
10
votes
2 answers

Jest expect an object with functions inside

I am writing a Jest test in which I call a function and expect an object in return as follows: const repository = container => { const makeBooking = (user, booking) => { 'make booking function called' } const generateTicket = (paid,…
Ashif Shereef
  • 454
  • 1
  • 8
  • 24
9
votes
2 answers

How to mock fetch response using jest-fetch-mock

I am calling api in the the react functional component. I am writing unit test cases of the component. I have mocked fetch using jest-fetch-mock. global.fetch = require('jest-fetch-mock'); Component.ts const Component = () => { useEffect(() =>…
Ajay S
  • 48,003
  • 27
  • 91
  • 111
9
votes
1 answer

jest.mock(..) not working in 'describe' (TypeError: moduleName.split is not a function)

jest.mock(..) does not seem to work at the 'describe' level for my tests. If I have the following : import React from 'react'; import {someFunction} from "./something/someFile"; describe('Overview Test', () => { jest.mock(someFunction); …
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
9
votes
3 answers

How to unit test a React component that renders after fetch has finished?

I'm a Jest/React beginner. In jest's it I need to wait until all promises have executed before actually checking. My code is similar to this: export class MyComponent extends Component { constructor(props) { super(props); …
Antonis Christofides
  • 6,990
  • 2
  • 39
  • 57
7
votes
0 answers

"jest-fetch-mock" with Typescript: "no call signature" and "doesn't exist on object" errors

jest-fetch-mock has a Typescript setup guide which I've followed: https://www.npmjs.com/package/jest-fetch-mock // package.json , "devDependencies": { "@types/jest": "^24.0.23", "jest": "^24.9.0", "jest-fetch-mock": "^2.1.2", …
Sean D
  • 3,810
  • 11
  • 45
  • 90
7
votes
1 answer

Jest TypeError: is not a constructor in Jest.mock

I am trying to write a unit test case using jest and need to mock the below pattern . I am getting TypeError: is not a constructor. Usecase : My usecase is as mentioned below MyComponent.js : import serviceRegistry from…
girish TS
  • 209
  • 1
  • 3
  • 10
6
votes
1 answer

React Native: Simulate offline device in Jest unit test

I'm writing a React Native app and I'm using Jest to unit test my code. I've written a function than checks if there is an internet connection. I know want to write it's unit tests. I'm stuck, because I can't figure out how to mock the connection…
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
6
votes
0 answers

How to write a test case for Touchable highlight onpress function in react-native using jest and enzyme

So,Here is my code for touchablehighlight,I used simulate for calling a onpress function but is is not working can anyone help me .
6
votes
1 answer

fetch-mock with jasmine not triggering then

I have this constant: export const clientData = fetch(`${process.env.SERVER_HOST}clientData.json`) .then(response => response.json()); Which works properly, and Now I'm working on the test of this, with Jasmine and fetch-mock This is my…
Pablo
  • 9,424
  • 17
  • 55
  • 78
4
votes
2 answers

Jest - How To Test a Fetch() Call That Returns A Rejected Promise?

I have the following function that uses fetch() to make an API call: export async function fetchCars(dealershipId) { return request('path/to/endpoint/' + dealershipId) .then((response) => { if (response.ok === false) { return…
Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
4
votes
1 answer

Testing a fetch.catch in custom hook

I've got this custom hook: import React from 'react'; import { useMessageError } from 'components/Message/UseMessage'; export interface Country { code: string; name: string; } export default function useCountry(): Array { const…
4
votes
1 answer

Error when testing fetch in vanilla JavaScript with jest-fetch-mock

I'm trying to test a fetch from a class. I need to create a class instance to access the fetch method. If I eliminate the class and return directly, the method works. I tried to do the test this way, but it returns undefined. Any solution? Thank you…
3
votes
1 answer

How to do jest mocking for non-exist external dependency

I am trying to mock external dependency which is not yet published in npm repository. import Utils from 'external-dependency'; jest.mock('external-dependency', () => ({ default: ()=> jest.fn() })); Above jest mocking display following error since…
Prakash Gupta
  • 203
  • 1
  • 4
  • 13
3
votes
1 answer

Unable to mock API request

Having a really hard time setting up jest-test-mock in in my TypeScript project. I was wondering if someone could point me in the right direction? I've got a function that looks like this: retrieveData.ts import fetch from 'cross-fetch'; export…
James Ives
  • 3,177
  • 3
  • 30
  • 63
1
2 3 4 5 6 7