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.