我正在开发一个包含后端 API 和 react/redux 前端应用程序的新项目。我正在使用 jest/enzyme/moxios 设置我的第一个测试。
我的问题是,当 api 服务器未运行时,测试返回“网络错误”,但如果服务器启动,所有测试都可以。
这是我的笑话测试(CRA):
import React from "react";
import { mount } from "enzyme";
import moxios from "moxios";
import Root from "../Root";
import App from "../App";
beforeEach(() => {
moxios.install();
moxios.stubRequest("*/products", {
status: 200,
response: [
{
id: 0,
name: "Test product 0",
description: "Test description 0",
price: 55.6
},
{
id: 2,
name: "Test product 2",
description: "Test description 2",
price: 55.6
}
]
});
});
afterEach(() => {
moxios.uninstall();
});
it("can fetch a list of products and display them", done => {
const wrapped = mount(
<Root>
<App />
</Root>
);
moxios.wait(() => {
wrapped.update();
console.log(wrapped.find("li").html());
expect(wrapped.find("li").length).toEqual(2);
done();
wrapped.unmount();
});
});
似乎 moxios 没有按预期工作,但我找不到原因......我还检查了 axios 适配器是 moxios:
console.log src/__tests__/productsIntegration.test.js:38
[Function: mockAdapter]