Giter Site home page Giter Site logo

Comments (4)

wheresrhys avatar wheresrhys commented on August 15, 2024

Hello,

Before fixing this it'd be good to know which environment it's happening in as the current implementation should work so I'd like to understand a bit more about why it's not. Where are you getting Request from? window.Request or somewhere else?

from fetch-mock.

aaronmars avatar aaronmars commented on August 15, 2024

This is in a node environment, with isomorphic-fetch loaded beforehand.

This should replicate the issue for you.

require('isomorphic-fetch');
var fetchMock = require('fetch-mock');
fetchMock.mock('http://example.com/', '{"foo": "BAR"}');
var request = new Request('http://www.example.com', { method: 'GET' });
fetch(request).then(function(response) {
    return response.text();
}).then(function(responseText) {
    console.log(responseText);
});

That final console log outputs the html from example.com, rather than the mocked JSON.

from fetch-mock.

wheresrhys avatar wheresrhys commented on August 15, 2024

Ah now I see the problem. I guess you're not on npm 3 yet, which means that isomorphic-fetch and fetch-mock don't share an instance of node-fetch unless you call npm prune.

I'm not sure whether to fix this with your suggested solution or to add a suggestion to run npm prune when not working in npm 3+. The downsides of your solution as I see it are:

  1. If fetch is accidentally called with an object or number, say, then, normalize request will return {url: undefined, options: undefined}, which could lead to some difficult to debug test failures
  2. The primary use case for this module is to mock fetch in the browser, where the problem you're reporting won't occur

On the other hand, your solution does basically work. Could you run npm prune and see if that fixes your problem?

from fetch-mock.

aaronmars avatar aaronmars commented on August 15, 2024

Well, npm prune didn't work. I even nuked npm_modules, reinstalled, and saw the same problem.
Updating npm worked though!

I was on [email protected], now I'm using [email protected]. That's fine for me.

As for the fix, I'm fine with "update npm" being the answer. I totally agree with the loosey-goosey nature of the fix I suggested, and prefer the code as-is.

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.