我正在使用React Test Renderer和Jest测试我的React Native应用程序。下面是我所做的简单代码示例:
PS:作为一个合理性检查,我还尝试了
我使用React Test Renderer与Jest来测试我的React Native应用程序。
这里是我正在做的简单代码示例:
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);
});
第一次测试失败,显示:
Expected: 1
Received: 2
第二个测试也失败了:
Expected: 2
Received: 4
为什么使用findAllByProps
时,React测试渲染器会找到双倍的实例?PS:作为一个合理性检查,我还尝试了
findByProps
,它可以工作。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);
});