Giter Site home page Giter Site logo

Comments (9)

SpaceviewMob avatar SpaceviewMob commented on August 15, 2024 1

We had the exact same issue. Found a solution, but it required a bit of extra code that we have now shared.

https://github.com/spaceviewinc/fetch-mock-forwarder

from fetch-mock.

theosherry avatar theosherry commented on August 15, 2024 1

Hey, I was having similar problems, and I resolved it by importing isomorphic-fetch to the global namespace rather than importing it to a variable. I see in your code that you were importing it to a variable as well, so this might be a solution for you.

I think isomorphic-fetch decorates or patches the already existing global fetch, so importing to a variable causes problems.

from fetch-mock.

wheresrhys avatar wheresrhys commented on August 15, 2024

From reading your code it looks like you're not interested in analysing calls to fetch, just in returning a mock response - is that right? In that case you could just return new Response(url, opts) using code similar to https://github.com/wheresrhys/fetch-mock/blob/master/src/fetch-mock.js#L17 and leave the call to fetch out of your implementation.

I could expose my mockResponse function as a static helper method if that'd be useful

from fetch-mock.

deep-c avatar deep-c commented on August 15, 2024

Hi,

Yea thats right.

I managed to get this working by using window.fetch instead of just fetch. From what i thought, fetch (from isomorphic fetch) for the browser would use that from the global window object which it patches if necessary (think i read that in the docs somewhere). It seems that fetch being referenced in my code isnt the same fetch as that of the browser window object (which upon logging in the console returns the mock function).

This just means that i have to annoyingly reference window.fetch until i get my backends up and then change all references to fetch when i dont need the mock response any longer and I need to start rendered things server side.

from fetch-mock.

wheresrhys avatar wheresrhys commented on August 15, 2024

I'd happily accept PR to document this in the troubleshooting section of the readme

from fetch-mock.

the-bass avatar the-bass commented on August 15, 2024

Hi!
I am having a very similar issue, also with a react/redux/webpack application, and am running out of ideas. I try to use fetch-mock in a unit-test, that's supposed to test a function I import from another file, but a real request is being performed (I've also described the problem more exact on StackOverflow). Can anyone please give me a hint on what I am missing?

from fetch-mock.

marc-sandb avatar marc-sandb commented on August 15, 2024

@SpaceviewMob solution worked for me

from fetch-mock.

the-bass avatar the-bass commented on August 15, 2024

Fantastic, it works for me, too! Thank you @marc-romera and @SpaceviewMob !

from fetch-mock.

wheresrhys avatar wheresrhys commented on August 15, 2024

FWIW I think @SpaceviewMob's solution, while it works, is an anti-pattern. In the browser fetch is supposed to be a global, as defined by the whatwg standard - any modules you're importing to provide as a fetch implementation should be considered as temporary polyfills for browsers that lack the native functionality. Assigning fetch to a variable departs from the standard, which means at some point in the future you will likely have to refactor.

I cannot see any good reason not to use fetch as a global in the browser, or am I missing something???

from fetch-mock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.