I wrote this test and it worked perfectly
describe('test', () => {
it( 'should render all text', () => {
const {getByText, getByTestId, debug} = render(
<View>
<View><Text>test1</Text></View>
<View><Text>test2</Text></View>
<View><Text>test3</Text></View>
</View>
});
});
I wanted to test each text alone so I edited the code to be like this .. I expected to work but it gave me this error
describe('test', () => {
const {getByText, debug} = render(
<View>
<View><Text>test1</Text></View>
<View><Text>test2</Text></View>
<View><Text>test3</Text></View>
</View>
});
it('should render test1', () => {
debug();
expect(getByText('test1')).toBeTruthy();
});
it('should render test2', () => {
debug(); // return null
expect(getByText('test2')).toBeTruthy(); // fails
});
it('should render test13, () => {
debug(); // return null
expect(getByText('test3')).toBeTruthy(); // fails
});
});
npmPackages:
react: 16.9.0 => 16.9.0
react-native: 0.61.5 => 0.61.5
react-native-testing-library: ^1.13.0 => 1.13.0
react-test-renderer: ^16.13.1 => 16.13.1
It sounds like you're using RNTL v2, which runs cleanup() after each test. You'll need to move your render call to the test functions, even if it means more code to run – it's for the safety of running your component tests in isolation.
Most helpful comment
It sounds like you're using RNTL v2, which runs
cleanup()after eachtest. You'll need to move your render call to thetestfunctions, even if it means more code to run – it's for the safety of running your component tests in isolation.