简体   繁体   English

Vue 关于单元测试 - javascript

[英]vue about unit test - javascript

I saw many unit test examples testing real DOM elements.我看到了许多测试真实 DOM 元素的单元测试示例。

ex:前任:

expect(vm.$el.querySelector('div').textContent).toBe('xxx')

What's the purpose of checking the text of DOM elements?检查 DOM 元素的文本的目的是什么?

Someone says that you can test computed values, but aren't computed values tested by using nextTick ?有人说你可以测试计算值,但不是使用nextTick测试计算值吗?

Also, people say that HTML tags will affect the text of the element:另外,人们说 HTML 标签会影响元素的文本:

<htmlTag><a><p>{{user.name}}</p></a></htmlTag>

And what if the product is in multiple languages?如果产品有多种语言怎么办?

I believe that we should test data, state, functions, and elements which should display correctly in the HTML template.我相信我们应该测试应该在 HTML 模板中正确显示的数据、状态、函数和元素。

What are all you guys doing with Vue's unit test?你们都在用 Vue 的单元测试做什么?

You make different questions, but I will try to answer the 'general' question about Unit Testing in the DOM.您提出不同的问题,但我将尝试回答有关 DOM 中单元测试的“一般”问题。

Unit Testing in DOM elements can be useful for differents reasons, but the most importants usually are these:由于不同的原因,DOM 元素中的单元测试可能很有用,但最重要的通常是这些:

  • Test simple user actions测试简单的用户操作

    Example of simple user actions:简单用户操作示例:

    Clicking on a link that toggles the visibility of a DOM element Submitting a form that triggers the form validation (...) These actions can be easily tested by simulating DOM events单击切换 DOM 元素可见性的链接 提交触发表单验证的表单 (...) 这些操作可以通过模拟 DOM 事件轻松测试

  • Analyzing Network Characteristics分析网络特征

    Manipulating JavaScript to determine various network characteristics from within the browser.操纵 JavaScript 以确定浏览器内的各种网络特征。 An example here 这里有一个例子

  • Test DOM mutations & Manipulation测试 DOM 突变和操作

    A good example explained here这里解释一个很好的例子

As you said, it's better to test directly the state / vars / methods, but sometimes the only way it's with the real or the virtual DOM.正如您所说,最好直接测试状态/变量/方法,但有时唯一的方法是使用真实或虚拟 DOM。

To learn more you can take a look at this (fantastic) guide on GitHub for Unit Testing in JavaScript here要了解更多信息,您可以Unit Testing in JavaScript此处查看GitHub 上的Unit Testing in JavaScript (很棒的)指南

Hope it clarify and helps you!希望它澄清并帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM