简体   繁体   中英

React native with Axios - post method with body returns timeout

I am attempting to create a miragejs server with a post request but am receiving a timeout every time I make a call. Strangely get/delete endpoints return fine but post/put endpoints hang indefinitely resulting in jest timeout error:

Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error

My miragejs server file:

import { createServer } from "miragejs";

export function startTestAPIServer() { 
    return createServer({
        namespace: "/testing",
        routes() {
            this.get("/test", (schema) => {
                console.log("recieved request!")
                return {"foo": "get testing"}
            });

            this.post("/test", (schema, request) => {
                return {"foo": "post testing"}
            })
        }
    });
}

My test file:

import { startTestAPIServer } from "../../content/mockAPI/testAPI"
import axios from "axios"

// initiate test server
let server;

beforeEach(() => {
  server = startTestAPIServer();
})

afterEach(() => {
  server.shutdown();
})


describe("testAPI", () => {
  it("returns get request", async () => {
    var data;
    await axios
        .get("/testing/test")
        .then((res) => data = res.data)
        .catch((err) => console.log(err));
    expect(data).toEqual({"foo": "get testing"});
  })

  it("returns post request", async () => { 
    var data;
    await axios
        .post("/testing/test", {})
        .then((res) => data = res.data)
        .catch((err) => console.log(err));
      
    expect(data).toEqual({"foo": "post testing"});
  })
})

The first case passes while the second fails. I'm not sure what I'm doing wrong that could cause something like this. I am pretty sure it is not axios causing the issue, as I've logged its request and returns, and only the request is ever logged. I'm also sure that the endpoint is in fact being hit, as console logs will print from within the endpoint code block, it simply won't return for some reason. Not sure if it's relevant, but I'm also running on react native.

This is a current issue with miragejs, see here . cesar-medina-25 found a fix by adding the following to the jest setup file.

function FormDataMock() {
  this.append = function () {};
}
global.FormData = FormDataMock;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM