跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • Jest 如何测试在 A 函数中调用的 B 函数?
未分類
24 5 月 2020

Jest 如何测试在 A 函数中调用的 B 函数?

Jest 如何测试在 A 函数中调用的 B 函数?

資深大佬 : Jack0112 1

如题,举个例子:

// ListItem.tsx interface Props {     onTouchStart: (e: React.TouchEvent) => void; }  const ListItem: React.FC<Props> = (props) => {     const handleTouchStart = (e: React.TouchEvent) => {         // 在组件内部函数调用传入的函数         props.onTouchStart(e);         // 执行一些其他操作     }      return <div onTouchStart={handleTouchStart}></div>; }  export default ListItem; 
// ListItem.test.tsx import { createElement } from 'rax'; import renderer from 'rax-test-renderer';  it('Test ListItem TouchStart', () => {     const mockFunc = jest.fn();     const component = renderer.create(<ListItem onTouchStart={mockFunc} />)     const tree = component.toJSON();     tree.eventListeners.touchstart();     // 报错     expect(tree.eventListeners.touchstart).toHaveBeenCalled(); }) 
expect(received).toHaveBeenCalled()      Matcher error: received value must be a mock or spy function      Received has type:  function     Received has value: [Function handleTouchStart] 

怎么在测试用例中测试传入props的函数是否存在? 怎么在测试用例中测试传入props的函数是否被调用?

大佬有話說 (3)

  • 資深大佬 : ayase252

    expect(mockFunc).toHaveBeenCalled()
    你应该测的是你的 spy

  • 主 資深大佬 : Jack0112

    @ayase252 怎么修改一下?能指点一下嘛?

  • 主 資深大佬 : Jack0112

    @ayase252 不好意思,没仔细看,我试一试

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具