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回答

张轩

2021-01-27

同学你好 我在做课程的时候 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

0
2
ywang04
好的 老师 如果后面有机会要更新这个课程的时候 也期待老师更新下这部分。感谢老师辛勤付出啦?
2021-01-28
共2条回复

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2128 学习 · 959 问题

查看课程