npm i和npm_让您的NPM套件包含Jest和Codecov☂️
npm i和npm
by Carl-Johan Kihl
卡尔·约翰·基尔(Carl-Johan Kihl)
让您的NPM套件包含Jest和Codecov☂️ (Get your NPM-package covered with Jest and Codecov ☂️)
介绍 (Introduction)
Let’s talk about code coverage, and how you can do coverage reports in Jest and Codecov.
让我们讨论一下代码覆盖率,以及如何在Jest和Codecov中进行覆盖率报告。
什么是代码覆盖率? (What is Code Coverage?)
If you’re familiar with testing. You know its main purpose:
如果您熟悉测试。 您知道它的主要目的:
Tests gives the developers freedom to make changes and refactor code with the confidence that everything should work fine as long as all the automated tests will pass.
测试使开发人员可以自由地进行更改和重构代码,并确信只要所有自动测试都能通过,一切就可以正常工作。
However, if the unit tests don’t cover all scenarios, there’s still a chance your changes can break something. That’s why we have Code coverage: the measure of how much of the code-base is covered by automated tests.
但是,如果单元测试不能涵盖所有情况,则您的更改仍有可能破坏某些东西。 这就是我们具有代码覆盖率的原因:自动化测试覆盖了多少代码库。
Without Code coverage analysis, your tests have lost their main purpose.
没有代码覆盖率分析,您的测试将失去其主要目的。
This is important when your project grows and many developers are involved.
当您的项目不断增长并且涉及许多开发人员时,这一点很重要。
✅ We can maintain quality of our test when new code is added. ✅ We get a deeper understanding of existing tests.✅ Give developers confidence to refactor code without worrying about breaking things. ✅ We can catch untested flows before they cause trouble.
when添加新代码后,我们可以保持测试质量。 ✅我们对现有测试有更深入的了解。✅使开发人员有信心重构代码,而不必担心会破坏代码。 ✅我们可以在未经测试的流量造成麻烦之前将其捕获。
Ok, now that we know what code coverage is, let’s implement it! ?
好的,现在我们知道什么是代码覆盖率,让我们实现它! ?
先决条件 (Prerequisites)
To keep this article short and concise, I will start here: Step by Step Building and Publishing and NPM Typescript Package.
为了使本文简短明了,我将从这里开始: 逐步构建和发布以及NPM Typescript软件包 。
What’s been done so far:
到目前为止已完成的工作:
✅ Setup a basic NPM-package✅ Add testing with Jest✅ Write a basic test
✅设置基本的NPM软件包 J用Jest添加测试✅编写基本测试
If you have your project already setup with Jest you’re good to go. ? If not, I recommend that you clone or fork the repository for this article to start off from a basic NPM-package foundation:
如果您已经使用Jest设置了项目,那就很好了。 ? 如果不是这样,建议您从本文的NPM软件包基础上开始克隆或构建本文的存储库:
git clone git@github.com:caki0915/my-awesome-greeter.git && cd my-awesome-greeter &&git checkout basic-package && npm install
If you’re interested how to build NPM packages, I recommend my previous article here.
如果您对如何构建NPM软件包感兴趣,建议在此推荐上一篇文章 。
Alright, now when everything is set up, let’s go!
好了,现在一切就绪,就开始吧!
在Jest中创建Coverage报告 (Create Coverage reports in Jest)
Creating coverage reports in Jest is easy. Just add this line in your jest config file:
在Jest中创建覆盖率报告很容易。 只需在您的jest配置文件中添加以下行:
"collectCoverage":true
collectCoverage: Should be set to true if you want jest to collect coverage information while running your tests. (Tests will run a little bit slower so it’s false by default.)
collectCoverage:如果希望在运行测试时开玩笑地收集覆盖率信息, 则应将其设置为true。 (测试运行会慢一些,因此默认情况下为假。)
Make sure your script command test
in your package.json file will run Jest with your config file.
确保package.json文件中的脚本命令test
将与配置文件一起运行Jest。
“test”: “jest --config jestconfig.json”
Alright! Run npm test
in your terminal, and voilà! You will have a new folder with code coverage files generated for you.
好的! 运行npm test
在您的终端上,瞧! 您将拥有一个新文件夹,其中包含为您生成的代码覆盖率文件。
Don’t forget to add the coverage folder to .gitignore
. We don’t want build-files in our repository. ?
不要忘记将coverage文件夹添加到.gitignore
。 我们不希望在我们的存储库中生成文件。 ?
使您的报告有用 (Make something useful of your reports)
Ok, that’s cool, we generated a folder with some files, but what should we do with this information? ?
好的,这很酷,我们生成了一个包含一些文件的文件夹,但是该如何处理这些信息? ?
First of all, you can manually review the coverage-report on a generated HTML-page. Open /coverage/lcov-report/index.html
in your browser:
首先,您可以在生成HTML页面上手动查看覆盖率报告。 在浏览器中打开/coverage/lcov-report/index.html
:
Ok, that’s nice, but do we REALLY need to manually review the reports on every build??
好的,这很好,但是我们真的需要在每个构建版本上手动查看报告吗?
No, you shouldn’t. You should publish the reports online to make something useful of them. In this article, we’re going to use a coverage reporting tool called codecov.io.
不,你不应该。 您应该在线发布报告以使它们有用。 在本文中,我们将使用一个称为codecov.io的覆盖率报告工具。
Codecov is free for open-source projects. It takes code coverage reports to the next level. With Codecov, we can also auto-generate badges and run it on continuous integration builds. (More on it later.)
Codecov对于开源项目是免费的。 它会将代码覆盖率报告提高到一个新水平。 借助Codecov,我们还可以自动生成徽章并在持续集成构建中运行它。 (稍后会有更多信息。)
Sign up at https://codecov.io/ and follow the guide to connect to Github and your repository. After that, you should end up seeing a screen like this:
在https://codecov.io/上注册,然后按照指南连接到Github和您的存储库。 在那之后,您应该最终看到如下屏幕:
Nice! For now, this page will be empty since you haven’t uploaded any reports yet, so let’s fix that. In the terminal, run:
真好! 目前,该页面将为空,因为您尚未上传任何报告,因此,请修复此问题。 在终端中,运行:
npm install --save-dev codecov
Normally you want to upload reports at the end of a continuous integration build, but for this article, we will upload the reports from our local machine. In the terminal run: (Replace <Your token> with your repository-token found in codecov.io)
通常,您希望在持续集成构建结束时上载报告,但是对于本文,我们将从本地计算机上载报告。 在终端运行中:( 用在编解码器 ov.io中找到的存储库令牌替换<您的令牌> )
./node_modules/.bin/codecov --token="<Your token>"
Success! Now you can view your report online in codecov.io.? ?
成功! 现在,您可以在codecov.io中在线查看报告。 ?
https://codecov.io/gh/<Github Username>/<Repository Name>/
将徽章添加到您的README.md (Add a Badge to your README.md)
Badges are important, especially for NPM packages. It gives the first impression of high quality when you see a beautiful code coverage badge in npmjs and Github.
徽章很重要,尤其是对于NPM软件包。 当您在npmjs和Github中看到漂亮的代码覆盖徽章时,它会给人以高质量的第一印象。
In your README.md add the following line:(Replace <Github Username>, <Repository Name> and <Branch Name> with your information)
在您的README.md中添加以下行:( 用您的信息替换<Github用户名>,<存储库名称>和<分支名称> )
[](https://codecov.io/gh/<Github Username>/<Repository Name>/)
In my case, it will look like this:
就我而言,它将如下所示:
[](https://codecov.io/gh/caki0915/my-awesome-greeter/)
Awesome! Now you can show the rest of the world that you are using unit-testing and code coverage reports! ? ?
太棒了! 现在,您可以向世界其他地方显示正在使用单元测试和代码覆盖率报告! ? ?
摘要 (Summary)
If you’re using tests, code coverage reporting is a must and it should run every-time you make a pull-request or make changes on your branches.
如果您正在使用测试,那么代码覆盖率报告是必须的,并且每次您执行拉取请求或在分支机构进行更改时都应运行。
You can find my NPM-starter package here on Github.It’s an educational base for best practices NPM-package development. Comments, Forks and PR’s are welcome. ?
您可以在Github上找到我的NPM-starter软件包。 它是NPM软件包开发最佳实践的教育基础。 欢迎评论,Forks和PR。 ?
下一步是什么? (What’s next?)
If you don’t use continuous integration (CI) yet, it’s time to set it up. In my next article, I’m going to cover continuous integration with code-coverage for NPM packages.
如果您尚未使用持续集成(CI),则该进行设置了。 在我的下一篇文章中,我将介绍NPM软件包的代码覆盖范围的持续集成。
If you find this article useful, please give it some claps and follow me for more articles about development.
如果您觉得本文有用,请鼓掌并关注我,以获取有关开发的更多文章。
祝您建立很棒的包裹好运! ? ? (Good luck building your awesome package! ? ?)
翻译自: https://www.freecodecamp.org/news/get-your-npm-package-covered-with-jest-and-codecov-9a4cb22b93f4/
npm i和npm
相关文章:

分页传页数的方法
<!DOCTYPE html><html> <head> <meta charset"UTF-8"> <title></title> </head> <body> <div> <span id"num">1</span> <button id"prev">上一页</button> <…

VUE input唤起键盘 底部固定的标签被顶上去解决办法
通过输入框的失去焦点事件和点击事件,当出现键盘时把绝对定位的底部文字隐藏,失去焦点(键盘隐藏的时候)显示底部文字 解决代码 <input type"text" class"weui-input" click"input_click" blur…

unity中摄像机的控制---调整摄像机,不让他摔倒
摄像机大部分人都会控制,最明显的就是让他旋转,随着鼠标的左右上下移动而旋转,但是总是会遇到一个问题,就是转着转着就仿佛他自己摔倒了似的,其实就是它本身绕着Z轴旋转了 这样就不是我们想要的结果了 要想解决也简单&…

java正则表达式课程_通过此免费课程学习正则表达式
java正则表达式课程by Beau Carnes通过博卡恩斯 通过此免费课程学习正则表达式 (Learn Regular Expressions with this free course) “Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.” -Jami…

Codeforces Round #370 (Div. 2)
A - Memory and Crow 这题我没看题意,看了样例猜了一下就AC了,题目好像还挺复杂的。 #include<bits/stdc.h> using namespace std; int a[100005]; int main() {int n;cin>>n;for(int i1;i<n;i) scanf("%d",&a[i]);for(int…

pat1004. Counting Leaves (30)
1004. Counting Leaves (30) 时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueA family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains…

css 引用字体
最近遇到个问题,页面使用的字体要用PingFangSC字体,引入方法如下: 简单介绍下PingFangSC字体: (1)苹方-简 常规体 font-family: PingFangSC-Regular, sans-serif; (2)苹方…

系统技术方案 系统构架_构架系统时应注意的事项
系统技术方案 系统构架by Ayelet Sachto通过Ayelet Sachto 架构系统时要记住的6件事 (6 Things to keep in mind when architecting a system) Architecture may sound like a “scary ” or overwhelming subject, but actually, applying logic and approaching the problem…

[LeetCode] Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num 38, the process is like: 3 8 11, 1 1 2. Since 2 has only one digit, return it. 分析一:最简单的循环方法 class Solutio…

vue 点击事件执行多次
把 click 改成 click.once 就可以了 示例代码 click.once"down" 这样有一个弊端,就是事件只执行一次就不再执行了, 另一种方式,做一个定时器 //默认设置dddown为 true if(that.dddown){that.dddown falsesetTimeout(function(…

如何以及为什么使用Android Visibility Listeners
The Android UI is built up from Views, and in a regular application, there are usually several of them. To find out which View the user is currently looking at, you need to install Visibility Listeners.Android UI是从Views构建的,在常规应用程序中&…

在vue中使用Element-UI
Element-UI是一套基于Vue2.0的UI组件库,http://element.eleme.io/#/zh-CN/component/carousel 首先npm install element-ui --save 然后在main.js中引入: import Vue from vue import ElementUI from element-ui import element-ui/lib/theme-default/in…
Flex布局教程(来源:阮一峰)
网页布局(layout)是 CSS 的一个重点应用。Flex 布局将成为未来布局的首选方案。本文介绍它的语法,下一篇文章给出常见布局的 Flex 写法。网友 JailBreak 为本文的所有示例制作了 Demo,也可以参考。 以下内容主要参考了下面两篇文…

ibatis的there is no statement named xxx in this SqlMap
报错情况如下:com.ibatis.sqlmap.client.SqlMapException: There is no statement named Control.insert-control in this SqlMap. at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.getMappedStatement(SqlMapExecutorDelegate.java:231)at com.ibatis.sq…

javascript案例_如何在JavaScript中使用增强现实-一个案例研究
javascript案例by Apurav Chauhan通过Apurav Chauhan 如何在JavaScript中使用增强现实-一个案例研究 (How to use Augmented Reality with JavaScript — a case study) In this experiment, I talk about how Augmented Reality with JS can be used to make learning more f…

久未更 ~ 一之 —— 关于ToolBar
很久没更博客了,索性开一个久未更 系列 > > > > > 久未更 系列一:关于ToolBar的使用(后续补充) 1 //让 ToolBar 单独使用深色主题 使得 toolbar 中元素 变为淡色 2 android:theme"style/ThemeOverlay.AppCompat.Dark.ActionBar"…
SQLServer怎样把本地数据导入到远程服务器上(转载)
平常用到mssql时间比较少,总是过一段时间就忘记应该怎么操作了。当要做mssq把本地数据导入到远程服务器的时候,就去网上搜索很久都没有图解的,所以今天自己收集一下免得下次又到处去找。希望对自己,同时对其它需要的人都有一定的帮…

input 默认样式的修改
/* 修改input选中的默认边框样式 */ outline: none; /* 修改input的选中时的光标颜色 */ caret-color:red; /* 修改input的选中时的默认边框 */ border: none; /* 修改input的提示文字的默认样式 */ input::-webkit-input-placeholder{color:#d0d0d0;}

巨石加密_点餐:如何吃一个可怕的巨石
巨石加密by Alan Ridlehoover通过艾伦里德尔霍弗 点餐:如何吃一个可怕的巨石 (Ordering Take Out: How to Eat a Scary Monolith) Martin Fowler said:马丁福勒(Martin Fowler) 说 : Almost all the successful microservice stories have started wit…

Halcon学习之六:获取Image图像中Region区域的特征参数
area_center_gray ( Regions, Image : : : Area, Row, Column ) 计算Image图像中Region区域的面积Area和重心(Row,Column)。 cooc_feature_image ( Regions, Image : : LdGray, Direction : Energy,Correlation, Homogeneity, Contrast ) …

dos下命令行执行程序时候注意程序所使用文件的路径问题
dos下命令行执行程序时候,最好是用cd命令先切换到程序所在目录下,这样就不会出现文件找不到的问题,如果由于特殊原因,不使用cd命令,而只使用路径命令时候程序中访问的资源也只能是改成绝对路径了,这样对有源…

Vant 使用之Toast Vant安装和使用
Vant 是一个VUE 的移动端组件库,里面有很多好用的组件。 第一步,安装和配置 Vant npm i vant -S npm i babel-plugin-import -D 安装完成之后,在项目 .babelrc 文件修改配置 plugins "plugins": [["import", {"…

15-5重构_重构-糟糕,我一直在向后做。
15-5重构by Justin Fuller贾斯汀富勒(Justin Fuller) 重构-糟糕,我一直在向后做。 (Refactoring — oops, I’ve been doing it backwards.) Welcome to my intervention. I’m a refactoring addict and I’m not afraid to admit it, but there’s only one prob…

JPush 使用教程
JPush 使用教程 自己使用的一些经验,为了方便直接从这里复制过去就行。 就当做个笔记,防止长时间忘记之后,还需要去官网看文档。 主要思路: sdk文件 三方依赖系统库 头文件 添加代理 初始化代码 1.版本信息 JPush : 2.2.0 Xco…

浏览器常见兼容性问题汇总
1、随便写几个标签,不加样式控制的情况下,各自的margin 和padding差异较大,解决方案是:*{margin:0;padding:0;} 2、块属性标签float后,又有横行的margin情况下,在IE6显示margin比设置的大,常出现…

VUE 动态绑定class
第一种:通过一个布尔值判断样式类是否生效 //isActive 是在data里面布尔值, rotateRight 是 class 样式类 //isActive 为true时样式类 rotateRight 生效 <div :class"{rotateRight:isActive}">abs</div> 第二种:通…

低声教育_我内心低声说:“成为建设者”
低声教育by Rebecca Radding由丽贝卡拉丁(Rebecca Radding) 我内心低声说:“成为建设者” (Something within me whispered: “Be the builder”) 加沙代码学院前任主持人Yasmin Hillis(自称嬉皮士)是一个内心的嬉皮士,她谈到了弗吉尼亚伍尔夫如何激发她…

【Web API系列教程】1.2 — Web API 2中的Action Results
前言 本节的主题是ASP.NET Web API怎样将控制器动作的返回值转换成HTTP的响应消息。 Web API控制器动作能够返回下列的不论什么值: 1。 void 2。 HttpResponseMessage 3, IHttpActionResult 4, Some other type 取决于返回的以上哪一种。…

前端开发常用单词
methods 方法 mounted 创建完成 export 输出 default 默认的 install 安装 components 组件 template 模板 params 参数 route 路线;途径 package 包;盒子;袋 ; toutes 路由器 plugin 插件 local host 本地 require 需要;依赖; storage 储存 prototype 原型 …

原生ajax的post操作
xml.open(方法,路径,是否开启异步); console.log(e);来找出数据所在位置; 调用 ajax只能传二进制或字符串,需要先把json转一下,JSON.stringify(); 获取到数据时我们要通过JSON.parse()再转成JSO…