fetch-mock allows mocking HTTP requests made using fetch, or any one of the many libraries imitating its api such as isomorphic-fetch, node-fetch and fetch-ponyfill.
Questions tagged [fetch-mock]
40 questions
17
votes
1 answer
How to mock several gets in fetch-mock?
I'm testing my react components and I want to mock several get operations. What I want to do is something like:
test(`Created correctly`, async () => {
fetchMock.get(`*`, JSON.stringify(FIRSTGETOBJ));
fetchMock.get(`*`,…

justHelloWorld
- 6,478
- 8
- 58
- 138
12
votes
1 answer
fetch-mock does not mock my fetch
here is the code snippet:
var fetch = require("node-fetch");
var fetchMock = require("fetch-mock");
function setupMockBlockChainExplorer() {
…

Rafael Korbas
- 2,213
- 3
- 19
- 30
10
votes
2 answers
How to setup fetch-mock with node-fetch, supertest and typescript
I am trying to add some jest tests to my node typescipt project. I would like to use supertest to call my koa router, but also use fetch-mock to mock requests that are made using node-fetch.
My solution so far is below, but the fetch in my router…

Joe
- 1,847
- 2
- 17
- 26
9
votes
2 answers
fetch-mock: No fallback response defined for POST
All my GET requests are going through but POST ones fail. This happens when I update fetch-mock from 7.3.0 to 7.3.1 or later.
console.warn Unmatched POST to url
Error fetch-mock: No fallback response defined for POST to url
http.js
export const get…

Manu
- 1,632
- 3
- 17
- 23
7
votes
0 answers
why does fetchmock restore give unmatched get?
I have written logic based on test cases already defined.Basically the tc checks for one server call below is the code.How do I modify my logic to make the tc pass?
this is test case:
it('there shall be only one server call in addFavourites()',…

curious coder
- 101
- 6
6
votes
1 answer
Use Fetch-Mock to return test Blob
I have a fetch which receives a file from a server and I am trying to mock the fetch for my tests with fetch-mock.
Using this code I can mock the endpoint and place the blob in the body:
const blob = new Blob(['a', 'b', 'c',…

HauptmannEck
- 165
- 1
- 11
5
votes
1 answer
TypeError: Cannot set property localStorage of # which has only a getter
I am mocking localStorage in unitests like
function storageMock() {
var storage = {};
....
}
and setting localStorage like
window.localStorage = localStorageMock()
It was working fine until, I have updated Node to 10.15.1.
It is throwing…

Tasawer Nawaz
- 927
- 8
- 19
5
votes
2 answers
How to reset fetch-mock for each test?
I have several React tests using Jest and fetch-mock, each one of them doing some get operations, so what I initially did was:
beforeAll(){
fetchMock.get(`*`, JSON.stringify(CORRECTRESPONSE));
}
However, in some tests I need to return wrong…

justHelloWorld
- 6,478
- 8
- 58
- 138
5
votes
1 answer
How do I fetch-mock a post request with a payload
I am using the wheresrhys fetch-mock npm module to run functional testing in my app. I would like to mock a fetch with method 'POST' and a specific payload.
It would look something like this:
fetchMock.mock({
routes: {
name:…

Paul
- 83
- 1
- 7
4
votes
0 answers
wrapper.children().debug() and .html() in Enzyme/Karma return different contents
Using fetchMock I want to make sure that the React component will display news stories from an external API. These tests were passing until I updated React from 15.4.2 to 16.4.2 and Enzyme from 2.9.1 to 3.7.0.
Issue is that when I run yarn run test…

k3ntako
- 51
- 1
- 6
4
votes
2 answers
fetch-mock mocking all requests
I'm using fetch-mock in order to mock some requests to the server. This is where all the requests are made:
import fetchMock from 'fetch-mock'
import initialState from 'src/initial-state'
if (process.env.NODE_ENV === 'development') {
…

kanedaki
- 306
- 1
- 5
- 16
3
votes
1 answer
How do I fetch-mock an accurate server error?
The issue:
fetchmock
fetchMock.get('glob:https://*/server-api-uri', () => {
throw { example: 'error' }
})
Source JS file:
exampleServerCall().catch(error => {
console.log(error) // error = "[ object object ]" :(
})
So my catch statements are…

Daniel Tonon
- 9,261
- 5
- 61
- 64
3
votes
2 answers
ECONNREFUSED 127.0.0.1:80 using fetch-mock-jest for /localRoute mock
So fetch-mock-jest is based on fetch-mock (it's not jest-fetch-mock..)
https://www.npmjs.com/package/fetch-mock-jest
http://www.wheresrhys.co.uk/fetch-mock/
The problem I have is:
// inside my test…

François Richard
- 6,817
- 10
- 43
- 78
3
votes
1 answer
How to get request's body with fetch-mock
I have tests which use fetch-mock to mock a post request with parameters in the body.
I can mock the request but i cannot find a way to return a response according to the request's body.
Lets say i have a post request with :
const body = {flag :…

Omer Ben Haim
- 595
- 4
- 14
2
votes
1 answer
Nock - Get number of calls performed to fetch
I've got a React custom hook that tries to avoid repeated calls to a URL. This is done by storing in state the isLoading prop, which is set to true whenever the fetch method is called, and is set to false once it receives the response from the…

Unapedra
- 2,043
- 4
- 25
- 42