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

问题描述

这是我的工具提示代码,可在MouseOver和Mouse Out上切换CSS属性 display:block display:none .

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不是函数

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-17 21:52