Giter Site home page Giter Site logo

Comments (9)

wheresrhys avatar wheresrhys commented on August 15, 2024 2

That'll work, but limiting responding to n calls I think is a nice feature to add anyway

from fetch-mock.

wheresrhys avatar wheresrhys commented on August 15, 2024 2

done

from fetch-mock.

wheresrhys avatar wheresrhys commented on August 15, 2024

Easiest way would be to manage the state outside the mock.

let called = false;
fetchMock.mock('http://test.com', function () {
   if (called) {
     return {valid: 'response', after: 'error'};
   }
   called = true;
   return 500;
})

(If that doesn't work it's a bug, so let me know and I'll fix it if it fails)

If you have any thoughts on what a nicer API for this sort of behaviour would look like I'm happy to consider adding it.

from fetch-mock.

wheresrhys avatar wheresrhys commented on August 15, 2024

I could accept an additional parameter specifying the number of times a mock should respond, but I'd be concerned about adding more complexity to the API and docs (which are already long enough IMO)

from fetch-mock.

queicherius avatar queicherius commented on August 15, 2024

Yeah the managing the state outside works, I was just wondering if I was overlooking something 😄

The times parameter would make this easier, but I am not sure if that usecase is so common that it justifies it.

from fetch-mock.

wheresrhys avatar wheresrhys commented on August 15, 2024

It's a feature I'd like to add anyway, though it does get a little bit tricky to implement in some of the details because it's permissable for calls to fall through from one route to another. Will have a go

from fetch-mock.

wheresrhys avatar wheresrhys commented on August 15, 2024

Some failing tests written https://github.com/wheresrhys/fetch-mock/compare/times, but the edge cases are quite problematic

from fetch-mock.

amit1911 avatar amit1911 commented on August 15, 2024

i have the same requirement and i am calling fetchMock.restore() before mocking another call to the same url and it seems to be working fine. Is this also okay?

from fetch-mock.

montogeek avatar montogeek commented on August 15, 2024

I would like to ask for this feature too, we can use something similar to Nock https://github.com/node-nock/nock#repeat-response-n-times

For example, this is useful when testing polling requests, first 3rd requests are the same, but 4th is different

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.