[Testing] Static Analysis Testing JavaScript Applications
The static code analysis and linting tool ESLint is the de-facto standard for linting JavaScript projects. In this lesson we’ll see how to install, run, and configure it for your preferences.
Install:
npm i -D eslint
Run:
npx eslint src
Create: .eslintrc file
{
"parserOptions": {
"ecmaVersion": "2018"
},
"extends": [
"eslint:recommended"
],
"rules": {
"no-console": "off"
},
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true
}
}
The code formatting tool prettier can help you avoid a lot of useless time spent formatting code and arguing about code formatting with your co-workers. It can also help you catch subtle issues with your code that you may not notice otherwise. In this lesson we’ll learn how to install and run prettier.
Install:
npm i -D prettier
Run:
"format": "prettier --write \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\""
npm run format
In VSCode: Settings.json
{
"editor.formatOnSave": true
}
Prettier is a pretty opinionated tool, but it does allow for some customization. In this lesson we’ll check out the prettier playground and see what options we want to enable in our project’s .prettierrc
file.
After adding in our custom configuration, we’ll create a .prettierignore
file so that you can avoid formatting any files generated within the project such as node_modules
or a build
folder.
.prettierrc:
{
"arrowParens": "avoid",
"bracketSpacing": false,
"insertPragma": false,
"jsxBracketSameLine": true,
"parser": "babylon",
"printWidth": 80,
"proseWrap": "always",
"requirePragma": false,
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": false
}
.prettierignore:
node_modules
coverage
dist
build
.build
.vscode
package.json
package-lock.json
debug.log
Because prettier can automatically fix a lot of stylistic issues in our codebase, it’s not necessary to have eslint check for those and it can actually be kind of annoying if it does. So let’s see how we can use eslint-config-prettier
to disable all rules that are made irrelevant thanks to prettier.
Install:
npm i -D eslint-config-prettier
.eslintrc:
{
"parserOptions": {
"ecmaVersion": "2018"
},
"extends": [
"eslint:recommended",
"eslint-config-prettier"
],
"rules": {
"no-console": "off"
},
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true
}
}
You can’t force everyone on your project to use the prettier integration for their editor, so let’s add a validation script to verify that prettier has been run on all project files.
Fortunately for us, Prettier accepts a --list-different
flag that you can use that will throw an error when code is not up to the standard of your project.
"scripts": {
"test": "node --require ./setup-global.js src/index.js",
"lint": "eslint src",
"format": "npm run prettier -- --write",
"prettier": "prettier \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\"",
"validate": "npm run lint && npm run prettier -- --list-different"
},
We can run:
npm run validate
to check whether there is any files which is not formatted.
If it is, then will throw error, we can run:
npm run format
to format the code.
ESLint can check for a lot of things, but it’s not a great tool for checking the types of variables that flow through your application. For this you’ll need a type-checking tool like Flow or TypeScript. Let’s see how we can configure our project to work with Flow.
Install:
npm i -D flow-bin
Add script:
"flow": "flow",
Run:
npm run flow
For the first time, we run 'npm run flow' will tell us to run:
npm run flow init
it create a .flowconfig file.
Create a exmaple js file to pratice flow:
//@flow function add(a: number, b: number): number {
return a + b
}
type User = {
name: {
first: string,
middle: string,
last: string,
},
}
function getFullName(user: User): string {
const {
name: {first, middle, last},
} = user
return [first, middle, last].filter(Boolean).join('')
}
add(1,2) getFullName({name: {first: 'Joe', middle: 'Bud', last: 'Matthews'}})
Run:
npm run flow
We won't see any error for this, if everything is correct type checking.
We have a 'validate' script setup previously:
"validate": "npm run lint && npm run prettier -- --list-different"
we can also add "flow" into it:
"validate": "npm run lint && npm run prettier -- --list-different && npm run flow"
In order to make 'validate' script works, we still need to install 'babel-eslint' for eslint to understand flow syntax:
Install:
npm i -D babel-eslint
.eslintrc:
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": "2018"
},
"extends": [
"eslint:recommended",
"eslint-config-prettier"
],
"rules": {
"no-console": "off"
},
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true
}
}
Then run:
npm run validate
it will checks eslint, prettier and flow.
We have a few checks we’ll run in continuous integration when someone opens a pull request, but it’d be even better if we could run some of those checks before they even commit their code so they can fix it right away rather than waiting for CI to run. Let’s use husky’s precommit script to run our validation.
Install:
npm i -D husky
Hook with 'precommit' with valdiation:
"precommit": "npm run validate"
If you don't want this precommit hook and commit you changes anyway you can do:
git commit -m "bad stuff" --no-verify
Rather than failing when a developer has failed to format their files or run linting on all the files in the project on every commit, it would be even better to just automatically format the files on commit and only check the relevant files with eslint. Let’s use lint-staged to run scripts on the files that are going to be committed as part of our precommit hook.
Install:
npm i -D lint-staged
package.json:
"precommit": "lint-staged && npm run flow"
.lintstagedrc:
{
"linters": {
"*.js": [
"eslint"
],
"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)": [
"prettier --write",
"git add"
]
}
}
Now when you do commit, it will automatcilly format all the code and readded to git and commit it.
[Testing] Static Analysis Testing JavaScript Applications的更多相关文章
- Unit Testing, Integration Testing and Functional Testing
转载自:https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional- ...
- Difference Between Performance Testing, Load Testing and Stress Testing
http://www.softwaretestinghelp.com/what-is-performance-testing-load-testing-stress-testing/ Differen ...
- WWDC: Thread Sanitizer and Static Analysis
Thread Sanitizer 过程 编译过程中链接了一个新的库.  也可以通过命令行来操作: $ clang -fsanitize=thread source.c -o executable $ ...
- Go testing 库 testing.T 和 testing.B 简介
testing.T 判定失败接口 Fail 失败继续 FailNow 失败终止 打印信息接口 Log 数据流 (cout 类似) Logf format (printf 类似) SkipNow 跳过当 ...
- [Unit Testing] Fundamentals of Testing in Javascript
In this lesson, we’ll get the most fundamental understanding of what an automated test is in JavaScr ...
- Penetration Testing、Security Testing、Automation Testing
相关学习资料 http://www.cnblogs.com/LittleHann/p/3823513.html http://www.cnblogs.com/LittleHann/p/3828927. ...
- [Unit Testing] AngularJS Unit Testing - Karma
Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. A ...
- 测试理论--branch testing and boundary testing
1 branch testing 分支测试 测试代码的所有分支 2 boundary testing 测试 程序的限制条件
- [React & Testing] Simulate Event testing
Here we want to test a toggle button component, when the button was click, state should change, styl ...
随机推荐
- 详解css媒体查询
简介 媒体查询(Media Queries)早在在css2时代就存在,经过css3的洗礼后变得更加强大bootstrap的响应式特性就是从此而来的. 简单的来讲媒体查询是一种用于修饰css何时起作用的 ...
- Python9-面对对象2-day23
#计算正方形的周长和面积 class Square: def __init__(self,side_len): self.side_len = side_len def perimeter(self) ...
- redis和memcached的优缺点及区别
1. 使用redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,li ...
- 【RAID】raid1 raid2 raid5 raid6 raid10的优缺点和做各自raid需要几块硬盘
Raid 0:一块硬盘或者以上就可做raid0优势:数据读取写入最快,最大优势提高硬盘容量,比如3快80G的硬盘做raid0 可用总容量为240G.速度是一样.缺点:无冗余能力,一块硬盘损坏,数据全无 ...
- [android开发篇]自定义权限
有时候,我们可能遇到如下需求场景:当用户在一个应用程序中进行某项操作时,会启动另外一个应用程序,最常见的时直接打开了另外一个应用程序,并进入其中某个Activity(如:有的应用中有推荐应用列表,当用 ...
- hdu 1527 威佐夫博弈
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- nginx,lvs,haproxy负载均衡对比
Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术,具体的应用需求还得具体分析. 如果是中小型的Web应用,比 ...
- [BZOJ3054] Rainbow的信号(考虑位运算 + DP?)
传送门 BZOJ没数据范围... 其实数据范围是这样的.. 前20%可以直接n^3暴力枚举每个区间 前40%可以考虑每一位,因为所有数每一位都是独立的,而和的期望=期望的和,那么可以枚举每一位,再枚举 ...
- HDU 5352 MZL's City (2015 Multi-University Training Contest 5)
题目大意: 一个地方的点和道路在M年前全部被破坏,每年可以有三个操作, 1.把与一个点X一个联通块内的一些点重建,2.连一条边,3.地震震坏一些边,每年最多能重建K个城市,问最多能建多少城市,并输出操 ...
- 【数位DP】bnuoj 52813 J. Deciphering Oracles
http://acm.bnu.edu.cn/v3/contest_show.php?cid=9208#problem/J [AC] #include<bits/stdc++.h> usin ...