本文介绍了react-testing-library 为什么 toBeInTheDocument() 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的工具提示代码,用于在 MouseOver 和鼠标退出 display: none 上切换 CSS 属性 display: block.

Here is my code for a tooltip that toggles the CSS property display: block on MouseOver and on Mouse Out display: none.

 it('should show and hide the message using onMouseOver and onMouseOut events respectively', () => {
    const { queryByTestId, queryByText } = render(
      <Tooltip id="test" message="test" />,
    )
    fireEvent.mouseOver(queryByTestId('tooltip'))
    expect(queryByText('test')).toBeInTheDocument()
    fireEvent.mouseOut(queryByTestId('tooltip'))
    expect(queryByText('test')).not.toBeInTheDocument()
    cleanup()
  })

我不断收到错误 TypeError: expect(...).toBeInTheDocument is not a function

I keep getting the error TypeError: expect(...).toBeInTheDocument is not a function

有没有人知道为什么会发生这种情况?我的其他渲染和快照组件测试都按预期工作.queryByText 和 queryByTestId 也是如此.

Has anyone got any ideas why this is happening? My other tests to render and snapshot the component all work as expected. As do the queryByText and queryByTestId.

推荐答案

toBeInTheDocument 不是 RTL 的一部分.您需要安装 jest-dom 以启用它.

toBeInTheDocument is not part of RTL. You need to install jest-dom to enable it.

这篇关于react-testing-library 为什么 toBeInTheDocument() 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 13:30