Angular DOM 测试

Angular DOM Testing

我在 DOM 测试和组件 class 测试之间进行选择。还有运行一些问题希望你能帮我解决。

  1. 优点和缺点在某种程度上对我来说很清楚,但我注意到 Github 中几乎所有的库都使用 DOM 测试,这背后有什么原因吗:D?。
  2. 通过 DOM 测试,是否有可能获得 html 覆盖率(类似于 Karma/Istanbul 的代码覆盖率)?
  3. 有人提到,在 DOM 测试的情况下,我正在测试一个场景而不是代码,是吗,如果是的话,这对我的期望有何影响(何时对我的测试感到满意)
  4. 在代码审查中,您会寻找什么(除了代码覆盖率)来确保添加的新内容经过全面测试?

提前致谢:)。

在测试前端时,理想情况下,您想要测试最终用户的体验。所以回应:

The Pros and Cons are to some point clear to me, yet I am noticing that nearly all the libraries in Github use DOM testing, is there a reason behind that :D ?.

DOM 测试允许您测试交互,这可能是大多数图书馆这样做的原因。它确保最终用户的交互执行预期的操作。

With DOM testing, is there any possibility to get html coverage ( similar to code coverage with Karma/Istanbul)?.

我不认为有类似 Karma/Istanbul 的“HTML 报道”报告,而且我不知道它会是 feasible/helpful。测试 angular 组件时,整个组件都在测试浏览器中呈现 - 因此对于呈现或测试了多少 HTML 没有非常有用的指标。​​

Some guy mentioned that, in case of DOM testing, I am testing a scenario not code, is that right, if yes how does that effect my expectations (when to be satisfied with my tests)

是 - 您正在测试场景和与 DOM 测试的交互。您设置用户可能遇到的场景,并测试用户在该场景中可以访问的操作。

In code review, what would you look for (other than code coverage) to make sure that the new stuff added is totally tested?

只要您确保您的测试有用并提供价值(它们正确评估用户的交互是否达到预期目标),您的 code-coverage 就足够了。除了组件、服务、指令等 unit 测试(我的建议),您还可以使用 Cypress.

等框架研究 E2E 测试

Testing Angular 是了解如何在 Angular 中考虑测试的重要资源。例如,它建议不要直接在组件上测试内部和私有方法,而是对组件的 API - 输入、输出、点击等隐式测试这些内部和私有方法进行单元测试。