Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React测试库:检查属性是否与regex匹配_Reactjs_Testing_Jestjs_React Testing Library - Fatal编程技术网

Reactjs React测试库:检查属性是否与regex匹配

Reactjs React测试库:检查属性是否与regex匹配,reactjs,testing,jestjs,react-testing-library,Reactjs,Testing,Jestjs,React Testing Library,RTL有没有办法测试元素是否具有基于正则表达式的有效属性 在我的情况下,我想检查每个 组成部分: const DropdownMenu = ({ open }) => { return ( <ul className="dropdown-menu"> {open && ( <> <li> <Link to="/link-

RTL有没有办法测试元素是否具有基于正则表达式的有效属性

在我的情况下,我想检查每个

组成部分:

const DropdownMenu = ({ open }) => {
  return (
    <ul className="dropdown-menu">
      {open && (
        <>
          <li>
            <Link to="/link-1/">Link 1</Link>
          </li>
          <li>
            <Link to="/link-2/">Link 2</Link>
          </li>
          <li>
            <Link to="/link-3/">Link 3</Link>
          </li>
        </>
      )}
    </ul>
  )
}
但Jest不接受我的正则表达式:

-- output :

Expected the element to have attribute:
href=/^\/.*\/$/
Received:
href="/link-1/"
使用

test('链接有开头和结尾的斜杠',()=>{
render()
const listLinks=screen.getAllByRole('link')
listLinks.forEach((el)=>{
expect(el).toHaveAttribute('href',expect.stringMatching(/^\/.\/$/))
})
screen.debug()
})

您是否阅读了该方法的相关内容?而是查看RTL文档。谢谢
-- output :

Expected the element to have attribute:
href=/^\/.*\/$/
Received:
href="/link-1/"
  test('links have beginning and trailing slashes', () => {
    render(<DropdownMenu open={true}></DropdownMenu>)
    const listLinks = screen.getAllByRole('link')
    listLinks.forEach((el) => {
      expect(el).toHaveAttribute('href', expect.stringMatching(/^\/.*\/$/))
    })
    screen.debug()
  })