[React Testing] Debug the DOM State During Tests using React Testing Library’s debug Function

风流意气都作罢 提交于 2020-05-01 13:17:45

While you’re writing your tests it can be helpful to see what the DOM looks like. You can do this with React Testing Library’s debug function which will log a formatted and syntax highlighted state of the DOM at the time it is called.

 

const { getByLabelText, debug } = render(<FavoriteNumber />)

 

Using it:

test('renders a text input with placeholder Search beer', () => {
  const { getByLabelText, debug } = render(<FavoriteNumber />)
  const input = getByLabelText(/favorite number/i)
  debug(input) // output input dom
  debug() // output whole component dom
  expect(input).toHaveAttribute('type', 'number')
})

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!