如何在 Jest 单元测试中模拟 React hook 的返回值
我有一个自定义挂钩 useAuth0
。我需要模拟钩子返回的特定函数(logout
)并保留其余函数的真实功能。这种情况有解决办法吗?
我试过:
jest.spyOn(useAuth0(), 'logout') // invalid hook call
jest.mock('lib/hooks/useAuth0', () => () => ({
...jest.requireActual('lib/hooks/useAuth0'),
logout: jest.fn()
})) // mocking the module instead of the returned value
expect(useAuth0().logout).toHaveBeenCalled() // fails - logout not called
I have a custom hook useAuth0
. I need to mock a specific function (logout
) returned by the hook and keep the real funcionality of the rest. Is there a solution for this case?
I tried:
jest.spyOn(useAuth0(), 'logout') // invalid hook call
jest.mock('lib/hooks/useAuth0', () => () => ({
...jest.requireActual('lib/hooks/useAuth0'),
logout: jest.fn()
})) // mocking the module instead of the returned value
expect(useAuth0().logout).toHaveBeenCalled() // fails - logout not called
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论