Dom-testing-library: Cannot getByText using —

Created on 17 Mar 2020  ·  4Comments  ·  Source: testing-library/dom-testing-library

  • DOM Testing Library version: 🤷‍♀️ Create-React-App
  • node version: v12.15.0
  • npm (or yarn) version: v6.13.4

Relevant code or config:

// Status.tsx
export default function Status ({ status }: StatusProps) {
  return (
    <div className='Status'>
      <span>&nbsp;&nbsp;{ status === 'NO_ATTEMPT' ? <>&mdash;</> : 'Log'}</span>
    </div>
  )
}

What you did:

// Status.test.tsx
test('should render mdash', () => {
  const { getByText } = render(<Status status={'NO_ATTEMPT'} />)
  expect(getByText(/&mdash;/i)).toBeInTheDocument()
})

What happened:

status should render `Log` text for each status value except NO_ATTEMPT

    Unable to find an element with the text: /&mdash;/. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

    <body>
      <div />
      <div />
      <div />
      <div>
        <div
          class="Status"
        >
          <i
            aria-hidden="true"
            class="root-41"
            data-icon-name="CircleFill"
            style="color: darkgrey;"
            title="Did not attempt"
          >
            
          </i>
          <span>

            —
          </span>
        </div>
      </div>
    </body>

      12 |       expect(getByText(/Log/i)).toBeInTheDocument()
      13 |     } else {
    > 14 |       expect(getByText(/&mdash;/)).toBeInTheDocument()
         |              ^
      15 |     }
      16 |     unmount()
      17 |   })

      at Object.getElementError (node_modules/@testing-library/dom/dist/config.js:34:12)
      at node_modules/@testing-library/dom/dist/query-helpers.js:71:38
      at getByText (node_modules/@testing-library/dom/dist/query-helpers.js:54:17)
      at forEach (src/tests/Status.test.tsx:14:14)
          at Array.forEach (<anonymous>)
      at Object.<anonymous> (src/tests/Status.test.tsx:9:16)

Reproduction:

https://codesandbox.io/s/react-testing-library-demo-q7ufl

Problem description:


Cannot match mdash text

Suggested solution:

Most helpful comment

was it trying to literally match the string &mdash; instead of the character it represents?

Yes. HTML is kinda quirky, and your users aren't going to look for &mdash; so I'm ok with having your test use instead.

All 4 comments

Hi @Ethan-Arrowood,

Try this: https://codesandbox.io/s/react-testing-library-demo-fllkl

expect(getByText(/—/i)).toBeInTheDocument();

Thank you @kentcdodds ❤️works like a charm

Any particular reason why the HTML codes didn't work? was it trying to literally match the string &mdash; instead of the character it represents?

was it trying to literally match the string &mdash; instead of the character it represents?

Yes. HTML is kinda quirky, and your users aren't going to look for &mdash; so I'm ok with having your test use instead.

Thank you!

Was this page helpful?
0 / 5 - 0 ratings