根据他自己的作者,这是答案react- testing-library
:
可能是JSDOM限制(在codeand@R_460_2419@中,它在实际的浏览器中运行)。实际上,问题在于css并未真正加载到JSDOM中的文档中。如果是这样,那将起作用。如果您想出一个自定义的笑话转换,可以在测试过程中将css文件插入文档中,那么您将被设置。
因此,如果您使用css-in-JS,这将起作用。
因此,基本上该import './TestItem.css'
测试中的部分将无法正常工作,因为JSDOM不会加载它,因此jest- dom
无法理解类的shouldHide
均值display: none
。
根据这个,您可以将CSS插入jsdom中:
import React from 'react'
import { render } from 'react-testing-library'
import TestItem from '../TestItem'
import fs from 'fs'
import path from 'path'
test.only('TestItem should render correctly', async () => {
const cssFile = fs.readFileSync(
path.resolve(__dirname, '../TestItem.css'),
'utf8'
)
const { container, getByText, debug } = render(<TestItem hide={true} />)
const style = document.createElement('style')
style.type = 'text/css'
style.innerHTML = cssFile
container.append(style)
const itemNode = getByText('Text')
debug()
expect(itemNode).not.toBeVisible()
})
然后测试应该通过。