wrapper和screen
来源:5-3 React 测试工具 - react-testing-library

ywang04
2021-01-26
const wrapper = render(<Button>Nice</Button>);
const element = wrapper.queryByText('Nice');
render(<Button>Nice</Button>);
const element = screen.queryByText('Nice');
老师 请问以上两种写法的区别 以及哪个更好呢? 谢谢
1回答
-
同学你好 我在做课程的时候 screen 这个api 还没有推出
从react-testing-library 作者的信息和更新来看,更推荐 screen,因为这更解决人类测试的流程,我们其实不需要一个wrapper,直接从 screen (屏幕)上获取信息。这是作者本人的话,请看下
The benefit of using screen is you no longer need to keep the render call destructure up-to-date as you add/remove the queries you need. You only need to type screen. and let your editor's magic autocomplete take care of the rest.
The only exception to this is if you're setting the container or baseElement which you probably should avoid doing (I honestly can't think of a legitimate use case for those options anymore and they only exist for historical reasons at this point).
最后是 stackoverlfow 关于这个问题的讨论,可以参考:
https://stackoverflow.com/questions/61482418/react-testing-library-screen-vs-render-queries
022021-01-28
相似问题