7
26 Comments

Dear frontend developers...how do you deal with mock APIs?

...I have a question for you: how do you deal with mock APIs?

Let's imagine a scenario that you need to build new shiny website in React/Vue.js/Angular/Svelte/WhateverIsCoolNow. You rely on backend devs to provide you with the API, but they are not ready.

Do you have any solution to build mock server?
If so, is it working locally on your computer, or in the cloud? How do you upload mock data/configuration to it? How do you share this server with your colleagues?
And most importantly, do you like this solution? What would you change?

If you're not mocking, why?
You don't need to - that's great. Lucky you!
If you cannot find best solution, then pls let me know what are your struggles, how the perfect mock solution should look and work like.

Thanks!

posted to Icon for group Developers
Developers
on April 27, 2020
  1. 7

    The simplest way to go is to use dummy data saved in a JSON file. There are tools which help you mock data , check out https://miragejs.com/ and https://github.com/typicode/json-server. I think MirageJS offers a nice developer experience. If you are planning to do something very custom, you could run your own server locally, but then you will have to check this into version control and this adds more overhead to you team mates as well.

    1. 1

      Thanks. Yeah, I've heard about both of them.
      Mirage is completely different approach. TBH I am not a fan of mixing mock code in production code, but this is me...

      So what is your perfect solution for mocking? I'm not sure if I got your point in last para. So adding to Git and more overhead. Is this good or bad in your opinion then?

      1. 1

        At work, we use JSON files and these files are used in the tests as well. This is one way to go. These files are checked into version control, so that solves the sharing problem as well.

        Regarding running your own server locally, I meant running a local Node server if you want total control over the mock server. I wouldn't recommend this unless you have specific requirements.

  2. 6

    I'm a backend developer and although your question was aimed at frontend devs, maybe my perspective will be useful.

    I work with a number of internal AP's and often the frontend team in miles further on that where the API is and therefore need the mocked interaction you're looking for.

    In our case, we're using Swagger Hub (https://swagger.io/tools/swaggerhub/) to build the API and in our specification we're including samples responses the endpoints can provide.

    Swagger Hub provides a URL (like https://virtserver.swaggerhub.com/MyCompany/MyApi/1.0.0) which you can use within your frontend application to continue building your application based on the mocked responses and later you can switch over the URL to an actual API server when it becomes available.

    This means we can take a day or 2 out with the frontend team and define the request and response data structures and give them something they can use fairly quickly even though that part of the API might be weeks away from being ready.

    The editor in Swagger Hub can be a bit clunky (it's not particularly easy to break the API down into smaller sections) and we're looking at Stoplight (https://stoplight.io) as an alternative product to write the specification in more manageable blocks and then import the full specification from Stoplight into Swagger Hub.

    1. 1

      Very useful - thanks.

  3. 2

    When I used to be in that situation 6 years ago, I just threw dummy data in a JSON file (or, in a few cases, into an XML file) and just fed that to my front-end as I built.

    Open source has grown an order of magnitude since then and now there are great utilities like JSON server that will basically do it for you! https://github.com/typicode/json-server

    1. 1

      Thanks for your message.
      Yeah, JSON-server is cool, but I think it's cool if you are working on the project just by yourself. They are offering hosted solution as well, this is something I am more interested in. Where you can share the mock with your team / co-workers.

      1. 3

        Why would you need to share a mock with them while you're waiting for them to finish writing the endpoints you're waiting on? That doesn't make sense at all...

        Is your goal here to see if people here want to pay for some idea you're thinking about building?

        If so, I strongly recommend flipping your strategy around and scouring the forum to see what people are already looking for! If you start with what you're thinking about making, a lot of people will be polite and say something like, "yeah I can see how that would be useful..."

        Those people will almost never buy from you. What you want is people looking so hard for something that they're starting threads on forums asking how to do X or what can do Y. Those people likely will pay for some thing to solve their problem.

        1. 1

          Im not that sneaky person, sorry :-). I simply have a task to build mock solution for my project. So main task is to work for this project for my client. Spec is quite loose, so I want to ask people what are they using.
          Notice that I posted in "Developers" forum, not "Ideas".

          Sure, IF I can build something that people would be interested to pay, I would like to sell it. But I have a task, I need to deliver mock server, so I asked what are people using. I need to build it anyway.

          Going back to your question about sharing. Why? One of options that client is considering is https://get.mocklab.io. JSON-Server is offering hosted solution https://my-json-server.typicode.com
          I believe this is useful when more than just one frontend developer is working on the project at the time.

          1. 1

            Got it, was just trying to save everyone some frustration. A lot of people on the forum are testing ideas to launch in every group, but I appear to have guessed wrong here.

            Going back to your question about sharing. Why?

            I just haven't ever needed anything like that. I don't think I've worked on the same component as another front-end dev at the same time, though.

            1. 2

              Many thanks for your reply.

  4. 1

    A lot of folks have mentioned using mock json files. While this works, it can be a bit of a pain to actually get that data to where you need it in your code (e.g. mocking your network layer).

    For testing, I use nock and I've thrown together some really janky code mashing jest and nock's nock-back mode together. It automatically records any API call made during tests and records the response. This lets me use prod (or, more often, and local instance of the app) to generate mock data for whatever constitutes third-parties to keep test instability down.

    For actually interacting with the mock data to iterate through development, I use storybook. By treating everything from just a button all the way up to a full page as a component with stories, I can get super-specific about testing the foundational components, but I can also look at pretty much the entire application without ever hitting a server.

    From there, typescript let's me mostly feel confident that when I pass API responses into those components, they'll render correctly. The key there, of course, is to either have a statically typed API or to mark all API responses as unknown so the typescript compiler forces you to write the runtime checks that would warn when unexpected data is received.

    1. 1

      Great, many thanks for detailed answer.

      1. 1

        No problem.

        I meant to mention: I've been eyeing pollyjs as a replacement for nock. nock is great, as far as it goes, but polly appears to be a lot more polished.

  5. 1

    mock server on the cloud

    1. 1

      Can you please clarify what you mean by "mock server"? Any specific service?

      1. 1

        My mock server is identical to my prod server. Except, all the data is mocked and lives in json files. It's is just a container running on Heroku. I use Node, GraphQL, and Prisma for this specific server

        1. 1

          Great. Many thanks for clarification.

  6. 1

    First of all, if you are writing proper website or big applications, ALL requests should be wrapped in dedicated services (basically classes with various methods like Users.getUser(x) etc).

    Why? What happens when the api gets updated? Or when backend guys decide to split their api into separate microservices? Or what if everyone drinks the cool-aid and wants to switch to graphql immediately?

    You don't want this changes to be visible in your codebase. So every such change should happen INSIDE dedicated services (if it's possible obviously).

    Now this immediately solves mock problem. You can have an instance of User service that serves static, json data (something like MockUsers implements Users).

    That's how we did it in REAL companies. Needles to say in my own projects I just want to build as fast as possible, so I ignore these rules, like many others.

  7. 1

    I built my own desktop tool for this: https://mockoon.com
    It can mock multiple REST API locally, act as a proxy (when you only want to mock some missing routes) and many other things.

    It became something bigger, and people like it apparently :)

    1. 1

      Damn, not gonna lie. This is something I am thinking of to build myself :D
      Looks very nice

      1. 1

        Thank you!
        There are a lot of competition on this market, but as long as you offer something different there is always enough space for everybody!

  8. 1

    I'm not a developer and I can't say about the mocking part, but our devs share their early stage work with we (product team) through ngrok (we use the free tier). I hope this can be useful somehow.

    Cheers

  9. 1

    Reiterating other's sentiments, I typically create a dummy JSON file. If I want to make sure my queries are proper, I've used graphcms to mock APIs before they're fully built. That being said, I'm strictly a GraphQL user 🤷‍♀️

    1. 2

      I'm looking for mock for REST API. Should mention this in the post.
      Thanks a lot for your feedback!

Trending on Indie Hackers
I'm a lawyer who launched an AI contract tool on Product Hunt today — here's what building it as a non-technical founder actually felt like User Avatar 150 comments A simple way to keep AI automations from making bad decisions User Avatar 65 comments Never hire an SEO Agency for your Saas Startup User Avatar 56 comments “This contract looked normal - but could cost millions” User Avatar 54 comments 👉 The most expensive contract mistakes don’t feel risky User Avatar 41 comments I spent weeks building a food decision tool instead of something useful User Avatar 28 comments