微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MSW 模拟服务器测试,如何才能通过?

如何解决MSW 模拟服务器测试,如何才能通过?

所以我在前端使用 React/redux,我正在尝试测试提交的表单是否将 redux 操作分派到服务器。 我正在尝试使用 MSW 模拟服务器并查看它是否已被调用,但它似乎无法正常工作。

form.test.tsx

test("dispatches createProfile action on submit",async () => {
    const testServer = setupServer()

    let req: any;
    testServer.use(
      rest.post("http://localhost:8000/profile",(_req,res,ctx) => {
        req = _req;
        return res(ctx.status(200));
      }),);

    const mockEmail = "123@mock.com";

    render(
      <browserRouter>
        <Form />
      </browserRouter>,);

    userEvent.type(screen.getByRole("textBox",{ name: "email" }),mockEmail);
    userEvent.click(screen.getByTestId("submit-button"));

    await waitFor(() => {
      expect(req).toBeTruthy(); //This is failing
    });
 });

所以基本上 req 应该是真实的。 我已经对端点进行了三次检查,并从高级开发人员那里查看了一些其他示例,并遵循(复制)了它。 我也试过 testServer.resetHandlers()

我似乎无法让它工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。