使用Tape和Vue Test Utils编写快速的Vue单元测试
by Edd Yerburgh
埃德·耶堡(Edd Yerburgh)
使用Tape和Vue Test Utils编写快速的Vue单元测试 (Write blazing fast Vue unit tests with Tape and Vue Test Utils)
Tape is the fastest framework for unit testing Vue components.
磁带是用于Vue组件进行单元测试的最快框架。
In this article, we’ll see how to write Vue unit tests with Tape and Vue Test Utils.
在本文中,我们将看到如何使用Tape和Vue Test Utils编写Vue单元测试。
This tutorial is for users familiar with unit testing. If you’re new to unit testing check out unit testing Vue components for beginners.
本教程适用于熟悉单元测试的用户。 如果您不熟悉单元测试,请为初学者查看单元测试Vue组件 。
什么是胶带? (What is Tape?)
Tape is a bare bones unit test framework that outputs the test report in TAP (Test Anything Protocol) format.
Tape是一个基本的单元测试框架,它以TAP (任何测试协议)格式输出测试报告。
It’s got a simple API to assert that your JavaScript and Vue components are behaving correctly.
有一个简单的API可以断言您JavaScript和Vue组件行为正确。
为什么要胶带? (Why Tape?)
A couple weeks ago I ran some performance tests on different testing frameworks. I wanted to find out which framework was the fastest for testing Vue SFCs (Single File Components).
几周前,我在不同的测试框架上进行了一些性能测试。 我想找出哪个框架是测试Vue SFC(单个文件组件)最快的框架。
I added Tape for completeness sake, and was shocked to find it was the fastest performing framework.
出于完整性考虑,我添加了Tape,但震惊地发现它是性能最快的框架。
To run tests in Tape, we need to do some setup. Let’s dive right in.
要在Tape中运行测试,我们需要进行一些设置。 让我们潜入。
引导项目 (Bootstrapping the project)
I’ve made a simple starter project to start with. Git clone the project into a directory:
我已经做了一个简单的入门项目。 Git将项目克隆到目录中:
git clone https://github.com/eddyerburgh/jest-vue-starter.git
cd
into it and install the dependencies:
cd
到它,并安装依赖:
cd jest-vue-starter && npm install
Run the dev server as npm run dev
to check out the app.
以npm run dev
身份运行开发服务器以检出该应用程序。
It’s pretty simple. The the main point of this tutorial is to see how to set up Tape and Vue, not to write complex tests.
很简单 本教程的重点是了解如何设置Tape and Vue,而不是编写复杂的测试。
设置Tape and Vue测试实用程序 (Setting up Tape and Vue Test Utils)
First thing to do is install Tape and Vue Test Utils:
首先要做的是安装Tape and Vue Test Utils:
npm install --save-dev tape @vue/test-utils
Vue Test Utils is in beta, so we need to request the version explicitly
Vue Test Utils是beta版,因此我们需要明确要求版本
Vue Test Utils needs a browser environment to run. This doesn’t mean we need to run the tests in a browser (thank fully!).
Vue Test Utils需要浏览器环境才能运行。 这并不意味着我们需要在浏览器中运行测试(完全感谢!)。
We can use jsdom to set up a browser environment in Node. It adds global variables like document
and window
to Node.
我们可以使用jsdom在Node中设置浏览器环境。 它将诸如document
和window
类的全局变量添加到Node。
jsdom is a bit of a pain to setup. Luckily some enterprising node developer made a wrapper library called browser-env
.
jsdom设置起来有些麻烦。 幸运的是,一些进取的节点开发人员制作了一个名为browser-env
的包装库。
npm install --save-dev browser-env
We need to run browser-env
before the tests. Tape lets us define files to run before the tests, we’ll do that in a sec.
测试之前,我们需要运行browser-env
。 Tape让我们定义了要在测试之前运行的文件,我们将在几秒钟内完成。
Vue SFCs need to be compiled before they’re tested. We can use require-hooks
to run WebPack on files when they’re required in Node. It’s a simple setup.
Vue SFC必须先进行编译,然后再进行测试。 当Node中需要文件时,我们可以使用require-hooks
对文件运行WebPack。 这是一个简单的设置。
First, install require-extension-hooks
and its variants:
首先,安装require-extension-hooks
及其变体:
npm install --save-dev require-extension-hooks require-extension-hooks-babel require-extension-hooks-vue
Let’s make that setup file I spoke about earlier. Create a test
directory, and add a setup.js
file. The final path will be test/setup.js
.
让我们制作我之前提到的安装文件。 创建一个test
目录,并添加一个setup.js
文件。 最终路径将是test/setup.js
。
We’re nearly there. Crazy stuff.
我们快到了。 疯狂的事情。
Let’s write a smoke test in Tape. Create a new file called List.spec.js
in the test directory. Full path test/List.spec.js
. Copy the test below into the file:
让我们在Tape中编写烟雾测试。 在测试目录中创建一个名为List.spec.js
的新文件。 全路径test/List.spec.js
将以下测试复制到文件中:
What’s going on there? We define a test
, and get a t
object in the callback. The t
object contains assertion methods. It also has a plan
method . We need to tell Tape how many tests it should expect.
那里发生了什么事? 我们定义一个test
,并在回调中获得一个t
对象。 t
对象包含断言方法。 它还具有plan
方法。 我们需要告诉Tape它应该进行多少测试。
Now we need a script to run the tests. Open the package.json
and add this script:
现在我们需要一个脚本来运行测试。 打开package.json
并添加以下脚本:
"unit": "tape ./test/specs/*.spec.js -r ./test/setup.js"
This tells tape to run all .spec
files in test/specs
. The -r
tells Tape to require
or run test/setup
before running our tests.
这告诉磁带运行test/specs
所有.spec
文件。 -r
告诉Tape在运行test/setup
之前require
或运行test/setup
。
Run the unit
tests:
运行unit
测试:
npm run unit
Yay, we have a passing test. But hoo boy—that’s some ugly test output ☹️
是的,我们的考试通过了。 但是hoo boy-那是一些难看的测试输出☹️
Remember I mentioned TAP earlier? This is TAP in it’s naked glory. Pretty ugly right? Don’t worry, we can prettify it.
还记得我之前提到过TAP吗? 这是TAP裸露的荣耀。 很丑吧? 不用担心,我们可以美化它。
Install tap-spec
:
安装tap-spec
:
npm install --save-dev tap-spec
We can pipe our TAP output from Tape. Edit the unit
script to pipe the output to tap-spec
:
我们可以通过管道传输TAP输出。 编辑unit
脚本以将输出通过管道传递到tap-spec
:
"unit": "tape ./test/specs/*.spec.js -r ./test/setup.js | tap-spec"
And run the tests again:
并再次运行测试:
npm run unit
Much better ?
好多了 ?
使用Tape and Vue测试实用程序编写测试 (Writing tests with Tape and Vue Test Utils)
Let’s write some tests then. Since we’re using Vue Test Utils, the tests are pretty readable.
然后让我们编写一些测试。 由于我们使用的是Vue Test Utils,因此测试可读性强。
In List.spec.js
, we’re going to write a test
that passes an items
array to List
. We’ll use the shallow
method from Vue Test Utils to shallow mount the component. shallow
returns a wrapper
containing the mounted component. We can use findAll
to search the render tree for<
li> tags, and check how many there are.
在List.spec.js
,我们将编写一个test
是通过一个items
数组List
。 我们将使用shallow
来自VUE考试utils的方法,以浅安装的组件。 shallow
返回包含已安装组件的wrapper
。 我们可以使用findAll
在渲染树中搜索<
li>标签,并检查有多少。
Copy the test from below into test/specs/List.spec.js
.
从下面将test/specs/List.spec.js
复制到test/specs/List.spec.js
Watch the tests pass with npm run unit
. Notice we have a custom error message for out t.equals
assertion. The default messages aren’t very readable, so it’s better to add our own.
使用npm run unit
观看测试通过。 请注意,对于t.equals
断言,我们有一个自定义错误消息。 默认消息不是很容易阅读,因此最好添加我们自己的消息。
Now add a new file test/specs/MessageToggle.spec.js
. In here we’ll write a test for, you guessed it, MessageToggle.vue
.
现在添加一个新文件test/specs/MessageToggle.spec.js
。 在这里,我们将为您猜测的MessageToggle.vue
编写一个测试。
We’re going to write two tests now. One will check the <
;p> tag renders a default message. We’ll use s
hallow again to get a wrapper containing the mounted component, and use th
e text method to return the text inside t
he <p> tag.
我们现在要编写两个测试。 将检查<
; p>标记是否呈现默认消息。 We'l l use s
再次空心来获得安装部件包含一个包装,和我们e th
e文方法返回文本insid et
他<p>标签。
The second test is similar. We’ll assert that the message changes when the toggle-message
button is pressed. To do that, we can use the trigger
method.
第二项测试类似。 我们将断言,按下toggle-message
按钮后消息会发生变化。 为此,我们可以使用trigger
方法。
Copy the code below into test/specs/MessageToggle.spec.js
:
将以下代码复制到test/specs/MessageToggle.spec.js
:
Run the tests again with npm run unit
. Woop—green tests ?
使用npm run unit
再次运行测试。 哇,绿色测试?
磁带的优缺点 (Pros and cons of Tape)
Now we’ve added some tests, let’s look at the pros and cons of using Tape.
现在,我们添加了一些测试,让我们看一下使用Tape的优缺点。
优点 (Pros)
It’s fast
它很快
Like really fast ?
喜欢真的快吗?
It’s simple
这很简单
You can read the source code to
您可以阅读源代码来
It uses TAP.
它使用TAP 。
Because TAP is a standard, there are lots of programs that work directly with TAP
因为TAP是一个标准,所以有许多程序可以直接与TAP一起使用
Like the tap-spec module, we just piped some TAP text into it and it prettified it for us
像tap-spec模块一样,我们只是将一些TAP文本通过管道传输到其中,并为我们美化了它
Limited assertions
有限断言
Limited assertions keep your assertions easy to understand
有限的断言使您的断言易于理解
缺点 (Cons)
Limited assertions
有限断言
This is a con too
这也是一个缺点
You can get useful error messages with assertions like
您可以通过以下断言获得有用的错误消息:
hasBeenCalledWith
, this is difficult to replicate witht.equal
hasBeenCalledWith
,很难用t.equal
复制It breaks
坏了
When you run more than 10000 tests
当您运行超过10000个测试时
You probably won’t hit that. But it might be a deal breaker for a large Vue project
您可能不会实现这一目标。 但这对于大型Vue项目来说可能会破坏交易
It’s basic
这是基本的
You’ll need to use other libraries for mocking, stubbing and faking
您需要使用其他库来进行模拟,存根和伪造
The pros and cons are pretty similar. Tape is basic, and that can be a good thing or a bad thing depending on who you ask.
利弊非常相似。 磁带是基本的,根据要问的人,磁带可能是好事也可能是坏事。
Most importantly though, it’s blazing fast!
最重要的是,它正在快速燃烧!
Fast unit tests are good unit tests.
快速的单元测试是好的单元测试。
呼吁采取行动 (Call to action)
The best way to work out a new test framework is to use it.
制定新测试框架的最佳方法是使用它。
On the next Vue project you start, try Tape. You can find a list of assertions on the Tape README.
在您启动的下一个Vue项目中,尝试使用Tape。 您可以在Tape README上找到断言列表。
Enjoy ?
请享用 ?
You can find the finished repo on GitHub.
您可以在GitHub上找到完成的仓库。
翻译自: https://www.freecodecamp.org/news/how-to-write-blazing-fast-vue-unit-tests-with-tape-and-vue-test-utils-be069ccd4acf/
相关文章:

js去除数组中重复值
//第三种方法加强版 Array.prototype.distinctfunction(){ var sameObjfunction(a,b){ var tag true; if(!a||!b)return false; for(var x in a){ if(!b[x]) return false; if(typeof(a[x])object){ tagsameObj(a[x],b[x]); }else{ if(a[x]!b[x]) return false; } } return ta…

CXFServlet类的作用
CXFServlet是Apache CXF框架中的一个核心组件,用于处理HTTP请求并将它们转换为Web服务调用。通过配置CXFServlet,你可以轻松地部署和管理SOAP和RESTful Web服务。

了解jvm对编程的帮助_这是您对社会责任编程的了解
了解jvm对编程的帮助by ?? Anton de Regt由?? 安东德雷格 这是您对社会责任编程的了解 (This is what you need to know about Socially Responsible Programming) 您的才华比银行帐户中的零值多 (Your talent is worth more than lots of zeroes in your bank account) L…

解压和生成 system.imgdata.img ( ext4格式)
另一篇文章讲述了如何解压和生成system.img, 那是针对yaffs2格式的文件系统镜像。 目前越来越多的Android手机放弃了nand, 更多采用了emmc为内部存储设备。 以emmc为存储设备的android手机,其文件系统(/system,/data两个分区)一般采用ext4格式…

简单分析beyond作曲
本人绝对是业余的哈 业余到什么水平呢?正在练习爬格子,还是一个星期练几次那种 先说下《海阔天空》 6,5,4,3 1,2,3,4 简单是简单得不得了,声从低到高,然后再从…

1 OC 对象的本质(一个NSObject 对象占用的内存大小)
1 前言 目录 1 前言 2 一个NSObject占用多少内存 3 为什么呢 ? 4 如何在内存中看呢? OC 的面向对象都是基于C/C 的数据结构实现的 结构体 2 clang 命令转换成c 代码 clang -rewrite-objc main.m -o main.cpp 以上的命令是不分平台进行编译的&…

Xiki:一个开发人员寻求增强命令行界面的能力
by Craig Muth通过克雷格穆斯(Craig Muth) Xiki:一个开发人员寻求增强命令行界面的能力 (Xiki: one developer’s quest to turbocharge the command line interface) I was sitting with my friend Charles in a trendy cafe next to Golden Gate Park in San Fra…

2 OC 对象的本质(一个Student 占用的内存大小)
一 Student 占用的内存空间 补充: 1 成员变量占用字节的大小: 2 内存对齐的规则:结构体的内存大小必须是最大成员变量的内存的倍数。 一个 Student 类,继承自NSObject,有两个属性,首先要知道,int 类型占用…

jdk动态代理源码学习
最近用到了java的动态代理,虽然会用,但不了解他具体是怎么实现,抽空看看了看他的源码。 说到Java的动态代理就不能不说到代理模式,动态代理也就是多了一个’动态’两字,在《大话设计模式》中不是有这句话吗?“反射&…

20162313苑洪铭 第一周作业
20162313苑洪铭 20016-2017-2 《程序设计与数据结构》第1周学习总结 教材学习内容总结 本周观看教材绪论 主要在教我建立一个简单的java程序 内容是林肯的名言 虽然看起来很简单 但是实际上问题重重 总而言之 这一周全是在出现故障的 教材学习中的问题和解决过程 教材学习好像并…

测试驱动开发 测试前移_测试驱动的开发可能看起来是工作的两倍-但无论如何您都应该这样做...
测试驱动开发 测试前移by Navdeep Singh通过Navdeep Singh 测试驱动的开发可能看起来是工作的两倍-但无论如何您都应该这样做 (Test-driven development might seem like twice the work — but you should do it anyway) Isn’t Test Driven Development (TDD) twice the wor…

3 OC 属性和方法
1 OC 的属性的生成 interface Student:NSObject {publicint _no;int _age;}property (nonatomic,assign)int height;end 当我们使用property 的时候,那么系统会自动的在其内部生成个属性 xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main.c…

ios绘图时的坐标处理
在iOS中,进行绘图操作时,一般主要是在UIView:drawRect中调用 UIGraphicsBeginImageContextWithOptions等一系列函数,有时候直接画图就行,比如UIImage的drawRect等,有时候需要进行稍微复杂的操作,比如颜色混…

mongoDB数据库操作工具库
/* Mongodb的数据库工具类 */ var client require(mongodb).MongoClient;function MongoUtil() { this.url"mongodb://localhost:27017/storage";//在本地新建数据库storage,此后插入的数据都在storage中 }MongoUtil.prototype.connectfunction(callback…

开源许可证 如何工作_开源许可证的工作方式以及如何将其添加到您的项目中...
开源许可证 如何工作by Radu Raicea由Radu Raicea 开源许可证的工作方式以及如何将其添加到您的项目中 (How open source licenses work and how to add them to your projects) Recently, there was some exciting news for developers around the world. Facebook changed t…

通过API文档查询Math类的方法,打印出近似圆,只要给定不同半径,圆的大小就会随之发生改变...
package question;import java.util.Scanner; import java.lang.Math;public class MathTest {/*** 未搞懂* param args*/public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("请输入圆的半径:");Scanner in new Scanne…

4 OC 中的内存分配以及内存对齐
目录 一 OC 中的内存分配 一 OC 中的内存分配 student 结构体明明是20?为什么是24个字节,因为结构体会按照本身成员变量最大的内存进行对齐,最大成员变量是8个字节,因此就是8的倍数,24个字节。 class_getInstanc…

JDE函数--GetUDC(B函数)
GetUDC使用方式: 转载于:https://www.cnblogs.com/GYoungBean/p/4117965.html

k8s crd构建方法_告诉您正在构建没人想要的东西的8种方法(以及处理方法)
k8s crd构建方法by Geoffrey Bourne杰弗里伯恩(Geoffrey Bourne) 告诉您正在构建没人想要的东西的8种方法(以及处理方法) (8 ways to tell you’re building something nobody wants (and what to do about it)) Building something users want is hard — damn hard. They ar…

iOS开发 - 线程与进程的认识与理解
进程: 进程是指在系统中正在运行的一个应用程序,比如同时打开微信和Xcode,系统会分别启动2个进程;每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内;线程: 一个进程要想执行任务,必须得有…

Winform开发中常见界面的DevExpress处理操作
我们在开发Winform程序的时候,需要经常性的对界面的一些控件进行初始化,或者经常简单的封装,以方便我们在界面设计过程中反复使用。本文主要介绍在我的一些项目中经常性的界面处理操作和代码,以便为大家开发的时候提供必要的参考。…

5 OC 中的三种对象
目录 OC 中对象的分类 一 instance 对象 二 类对象 三 元类对象 总结: OC 中对象的分类 instance 对象 类对象 元类对象 一 instance 对象 内存中包含哪些信息 isa 指针 其他成员的变量Student *stu1 [[Student alloc]init]; 以上的stu1 就是实例对象 二 类对象 以…

travis ci_如何使用Travis CI和GitHub进行Web开发工作流程
travis ciby Vijayabharathi Balasubramanian通过Vijayabharathi Balasubramanian 如何使用Travis CI和GitHub进行Web开发工作流程 (How to use Travis CI and GitHub for your web development workflow’s heavy lifting) It’s common to hack together apps on CodePen wh…

android.view.ViewRoot$CalledFromWrongThreadException的解决办法
android 是不允许子线程直接更新UI的,如果一定要在子线程直接更新UI就会出现android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.大概意思就是说 只有原来创建找个视图hierarchy的…

6 OC中 isa 和 superclass 的总结
目录 一 关于isa 和 superclass 的总结 二 为什么基类的metaclass 的superclass 指向的是基类的类 三 isa 的细节问题 总结如下: instance 的isa 指向是classclass 的isa 指向是metaclassmetaclass 的isa指向是基类的imetaclassclass 的superclass 指向的是父类…

opencv下指定文件夹下的图片灰度化(图片的读取与保存)-------简单记录
对于此功能其实很简单:主要是在c方面的字母数字的拼接问题存在一定的问题。C数字字母拼接问题: 1 #include <fstream> 2 #include <string> 3 #include <iostream> 4 #include "highgui.h" 5 #include <cv.h> 6 #…

css菜单缓慢滑动_如何使用HTML,CSS和JavaScript构建滑动菜单栏
css菜单缓慢滑动by Supriya Shashivasan由Supriya Shashivasan 如何使用HTML,CSS和JavaScript构建滑动菜单栏 (How to build a sliding menu bar using HTML, CSS and JavaScript) A menu is what you look for when you land at a website. It has options and gi…

素数环问题---深度搜索遍历
1264: 素数环 时间限制: 1 Sec 内存限制: 128 MB提交: 29 解决: 8[提交][状态][讨论版]题目描述 有一个长度为n的环形序列由1,2,3,...,n组成,环中相邻两个整数和均为素数。你需要找到所有满足条件的环。输入 输入n表示环的长度(n<16)输出…

android之Notification通知
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的。 package cn.com.chenzheng_…