访问jest模拟模块属性

qlckcl4x  于 2023-11-15  发布在  Jest
关注(0)|答案(1)|浏览(126)

现在我有这样的结构来嘲笑请求。
我想测试jest.mock内部的属性

import react from "react"

jest.mock("../../../api", () => ({
  useGetRequest: () => ({
    data: mockMyCards,
    isLoading: false,
})

it(() => {
render()
// how to test that useGetRequest had been called, how to access insides of jest.mock 
// expect(useGetRequest).toHaveBeenCalled()
})

字符串

wh6knrhe

wh6knrhe1#

将模拟的useGetRequest Package 在jest.fn()中。类似于:

// Notice the function below
const mockedUseGetRequest = jest.fn(() => ({ 
  data: mockMyCards,
  isLoading: false,
}));

jest.mock('../../../api', () => ({
  useGetRequest: () => mockedUseGetRequest(),
}));

describe('YourComponent', () => {
  it('should call useGetRequest', () => {
    render(<YourComponent />);
    expect(mockedUseGetRequest).toHaveBeenCalledTimes(1);
    // Your other assertions...
  });
});

字符串

相关问题