I'm using React Test Renderer with Jest to test my React Native app.
Here is a simple code example of what I'm doing:
it('renders one text', () => {
const text = 'bar';
const instance = renderer.create(
<View>
<Text testID="foo">{text}</Text>
</View>
).root;
expect(instance.findAllByProps({ testID: 'foo' }).length).toEqual(1);
});
it('renders two texts', () => {
const text = 'bar';
const instance = renderer.create(
<View>
<Text testID="foo">{text}</Text>
<Text testID="foo">{text}</Text>
</View>
).root;
expect(instance.findAllByProps({ testID: 'foo' }).length).toEqual(2);
});
The first test fails saying:
Expected: 1
Received: 2
And the second test also fails:
Expected: 2
Received: 4
Why does react test renderer using findAllByProps
find double the instances?
PS: As a sanity check I also tried findByProps
which works:
it('renders one text', () => {
const text = 'bar';
const instance = renderer.create(
<View>
<Text testID="foo">{text}</Text>
</View>
).root;
expect(instance.findByProps({ testID: 'foo' }).props.children).toEqual(text);
});