typescript 为什么Jest不监视函数?

zf9nrax1  于 2023-05-19  发布在  TypeScript
关注(0)|答案(1)|浏览(176)

我写了一个简单的自定义钩子:

import { IsValueAlreadyRegistered } from "../../entities/registration/actions";

export const useForgetPassword = () => {
  const activeStep = 0;
  const email = "any@any.com";

  const nextStep = (): void => {
    let isThereError: boolean = false;

    switch (activeStep) {
      case 0:
        if (!isThereError) {
          IsValueAlreadyRegistered(email, "isEmailRegistered");
        }
        break;
    }
  };

  return { nextStep };
};

它只有一个我导入的功能。我想监视它,并确保它是用预期的参数调用的。
这是我的测试:

import { renderHook } from "@testing-library/react";
import { useForgetPassword } from ".";
import * as Registered from "../../entities/registration/actions";

describe("useForgetPassword", () => {
  const { result } = renderHook(() => useForgetPassword());

  const IsValueAlreadyRegisteredMock = jest.fn();

  test("nextStep", () => {
    jest
      .spyOn(Registered, "IsValueAlreadyRegistered")
      .mockReturnValue(IsValueAlreadyRegisteredMock);

    const { nextStep } = result.current;
    nextStep();

    expect(IsValueAlreadyRegisteredMock).toBeCalledWith(
      "any@any.com",
      "isEmailRegistered"
    );
  });
});

我检查了导入函数IsValueAlreadyRegistered的路径和我想要监视的路径,它们匹配。
下面是函数代码IsValueAlreadyRegistered

import { registrationFormSlice } from ".";
import { AppDispatch } from "../store";

const { setEmail } = registrationFormSlice.actions;

export const IsValueAlreadyRegistered =
  (value: string, process: string) => async (dispatch: AppDispatch) => {
    switch (process) {
      case "isEmailRegistered":
        dispatch(setEmail({ value, error: false, text: "" }));
        break;
    }
  };

问题是Jest表示没有调用IsValueAlreadyRegistered函数。我不明白为什么?

我跟踪了代码的执行,并确保调用了函数IsValueAlreadyRegistered,并将我需要的参数传递给它:

我认为这可能是由于使用了调度:IsValueAlreadyRegistered中的AppDispatch,因为在执行代码时,调试器没有切换到IsValueAlreadyRegistered函数。但是,当你监视一个函数时,它必须执行该函数的代码吗?在我看来,不,它被替换为.fn()。
我也假设我可能用错了:

jest
      .spyOn(Registered, "IsValueAlreadyRegistered")
      .mockReturnValue(IsValueAlreadyRegisteredMock);

告诉我哪里出错了,我肯定是一些小事情,但我看不出来。非常感谢您提前任何帮助!

j8ag8udp

j8ag8udp1#

Assert是错误的,使用"any@any.com""isEmailRegistered"参数调用了Registered.IsValueAlreadyRegistered
Registered.IsValueAlreadyRegistered的mock返回值是IsValueAlreadyRegisteredMock函数,它没有被调用。你的代码似乎不正确,我猜IsValueAlreadyRegistered是一个基于其函数签名的redux形实转换。所以一个形实转换程序应该被正确地调用为:

const dispatch = useDispatch();
dispatch(IsValueAlreadyRegistered(email, 'isEmailRegistered'))

因此redux-thunk中间件将调用内部函数(IsValueAlreadyRegisteredMock)。
无论如何,检查正确的Assert。

describe("useForgetPassword", () => {
  const { result } = renderHook(() => useForgetPassword());
  const IsValueAlreadyRegisteredMock = jest.fn();

  test("nextStep", () => {
    jest
      .spyOn(Registered, "IsValueAlreadyRegistered")
      .mockReturnValue(IsValueAlreadyRegisteredMock)

    const { nextStep } = result.current;
    nextStep();

    expect(Registered.IsValueAlreadyRegistered).toBeCalledWith(
      "any@any.com",
      "isEmailRegistered"
    );
  });
})

相关问题