Description
react-testing-library
version: 2.1.0node
version: 8.9.3npm
(oryarn
) version:yarn
1.3.2
Relevant code or config
Component to be tested:
const Component = ({ currentStep }) => <div>Step {currentStep} of 4</div>
What you did:
Test code:
const { getByText } = render(<Component currentStep={1} />)
expect(getByText('Step 1 of 4')).toBeDefined()
What happened:
Unable to find an element with the text: Step 1 of 4. 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.
<div>
Step
1 // <--------- newline
of 4
</div>
Reproduction repository:
This can be reproduced in the current repository test cases by inserting a variable into the JSX.
Problem description:
Variables in JSX strings cause new line issues (and failing tests). I believe that in the spirit of "not testing implementation details", this should be considered a harmful problem. If I add an extra space around the "1", the test passes (period signifies space):
expect(getByText('Step..1..of 4')).toBeDefined() // passing
Suggested solution:
I don't know why this is happening, but I'm guessing it is something to do with how the component is being serialised. I'm also guessing that this issue should be here and not dom-testing-library but I don't know for sure where the root of the problem lies.