question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using mirageJS with Axios in an existing application

See original GitHub issue

Hey, I had the idea to use the mirage as a hybrid. I mean, to test it better I’ve created a POC to test the Mirage + Axios. However, I’m facing a few problems… it seems the request went through the mirage but it never reaches the Axios response going always to the final bracket.

I did the POC because I started using it in a big project, and we faced it, and I did not know how to conduct it.

I saw a few solutions which say to exchange the Axios per fetch. But in a big application, it could take more time than I expect. Is there a solution to work with both?

server.js

import { createServer } from "miragejs";

export function makeServer() {
  return new createServer({
    routes() {
      this.passthrough();
      this.passthrough("https://api.github.com/**");

      this.namespace = "/learning";
      this.urlPrefix = "http://localhost:3333";

      this.get("/api/users", () => [
        { id: "1", name: "Luke" },
        { id: "2", name: "Leia" },
        { id: "3", name: "Anakin" },
      ]);
    },
  });
}

App.js

import { useEffect, useState } from "react";
import { api } from "./axios";
import { makeServer } from "./server";

interface UserProps {
  avatar: string;
  blog: string;
  name: string;
}

interface UserFactProps {
  name: string;
  id: number;
}

function App() {
  const [user, setUser] = useState<UserProps>({} as UserProps);
  const [factUser, setFactUser] = useState<UserFactProps[]>([]);
  makeServer();

  useEffect(() => {
    try {
      api.get("/users/michenriq").then((response) => {
        setUser({
          avatar: response.data.avatar_url,
          blog: response.data.blog,
          name: response.data.name,
        });
      });
    } catch (err) {
      console.warn(err);
      console.warn("parou aqui");
    } finally {
      console.log("caiu aqui");
    }

    api.get("http://localhost:3333/learning/api/users").then((response) => {
      setFactUser(response.data);
    });
  }, []);

  return (
    <>
      <h1>{user.name}</h1>
      <p>{user.blog}</p>
      <img src={user.avatar} alt={user.name} />

      {factUser?.map((user) => {
        return <p key={user.id}>{user.name}</p>;
      })}
    </>
  );
}

export default App;

axios instance ps: I put the interceptors to understand how far the request gone

import axios, { Axios } from 'axios'

export const api: Axios = axios.create({
  baseURL: 'https://api.github.com'
})

I can do it using hybrid fetch to external api and axios to internal, but I figure it does not the right way to do that.

ps: these 3 items are the mirage mock not the GitHub api, tough

As the images below the request is made but we never got the response…

Screen Shot 2021-11-08 at 9 26 56 PM Screen Shot 2021-11-08 at 9 27 07 PM

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:17

github_iconTop GitHub Comments

5reactions
dribeirodevcommented, Nov 9, 2021

I have the same problem

4reactions
matrunchykcommented, Nov 11, 2021

Same issue. It works good with Fetch API, but doesn’t work with axios (tested on axios@0.24.0 and mirage@0.1.42).

It works good with mirage@0.1.41

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction - Mirage JS
Use a client-side interceptor to handle your app's network requests. Some HTTP clients come with mock adapters (e.g. axios-mock-adapter can be used to...
Read more >
Axios request that are sent via `passthrough()` don't resolve
Hi, I have a problem with using miragejs and axios together. When I send a request to a mocked route which has a...
Read more >
MirageJs: cannot use axios if I call api with passthrough
In my app runs a mirage server with mock api and a "passthrough" at my true server. When I dispatch "loginThunk" thunk, it...
Read more >
Mirage JS tutorial: Mocking APIs in React - LogRocket Blog
In this Mirage JS tutorial, we walked through how to mock APIs with Mirage JS and get data from the APIs in a...
Read more >
Vue the Mirage from this angle! - This Dot Labs
Mirage.js is a client-side server that embeds itself into the app. ... the axios library to allow us to communicate with a backend...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found