我试图创建一个带有post请求的miragejs服务器,但是每次我打电话时都会收到超时。奇怪的是,get/delete端点返回很好,但是post/put端点无限期挂起,从而导致玩笑超时错误:
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我的miragejs服务器文件:
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"}
})
}
});
}我的测试文件:
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"});
})
})第一种情况过去,而第二种情况失败。我不知道我做错了什么会导致这样的事情。我很确定这不是axios引起的问题,因为我已经记录了它的请求和返回,并且只有请求被记录下来。我还确信端点实际上正在被击中,因为控制台日志将从端点代码块中打印出来,因此由于某种原因它不会返回。不确定这是否相关,但我也在运行react本机。
发布于 2022-01-04 05:03:08
这是海市蜃楼当前的问题,请参阅这里。cesar-medina-25通过在jest安装文件中添加以下内容找到了一个修复程序。
function FormDataMock() {
this.append = function () {};
}
global.FormData = FormDataMock;https://stackoverflow.com/questions/70562089
复制相似问题