Skip to content

Commit 6751d5e

Browse files
committed
doc: add browser-testing.md and code-coverage.md translate
1 parent ea33c7a commit 6751d5e

File tree

3 files changed

+271
-2
lines changed

3 files changed

+271
-2
lines changed

zh_CN/docs/recipes/browser-testing.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
___
2+
**备注**
3+
4+
这是[browser-testing.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/browser-testing.md)的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/master...zhaozhiming:master)用来查看本翻译与AVA的master分支是否有差别(如果你没有看到`browser-testing`发生变化,那就意味着这份翻译文档是最新的)。
5+
___
6+
7+
# 设置AVA做浏览器测试
8+
9+
翻译: [Français](https://github.com/sindresorhus/ava-docs/blob/master/fr_FR/docs/recipes/browser-testing.md), [Русский](https://github.com/sindresorhus/ava-docs/blob/master/ru_RU/docs/recipes/browser-testing.md), [简体中文](https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/docs/recipes/browser-testing.md)
10+
11+
12+
13+
AVA[](https://github.com/sindresorhus/ava/issues/24)不支持在浏览器中运行测试。一些库要求浏览器指定全局变量(`window`, `document`, `navigator`等等)。
14+
React就是其中的一个例子,最低要求如果你想用ReactDOM.render和用ReactTestUTils模拟事件。
15+
16+
这个秘方让需要模拟浏览器环境的库可以工作。
17+
18+
## 安装jsdom
19+
20+
安装[jsdom](https://github.com/tmpvar/jsdom)
21+
22+
> 一个WHATWG DOM和HTML标准的JavaScript实现,给node.js使用的。
23+
24+
```
25+
$ npm install --save-dev jsdom
26+
```
27+
28+
## 设置jsdom
29+
30+
创建一个helper文件并放在`test/helpers`文件夹中,这样确保AVA不会把它当成测试来处理。
31+
32+
`test/helpers/setup-browser-env.js`:
33+
34+
```js
35+
global.document = require('jsdom').jsdom('<body></body>');
36+
global.window = document.defaultView;
37+
global.navigator = window.navigator;
38+
```
39+
40+
## 配置测试使用jsdom
41+
42+
配置AVA,将`require`设置为helper文件,这样每个测试运行前都会先加载它。
43+
44+
`package.json`:
45+
46+
```json
47+
{
48+
"ava": {
49+
"require": [
50+
"./test/helpers/setup-browser-env.js"
51+
]
52+
}
53+
}
54+
```
55+
56+
## 享受!
57+
58+
编写你的测试并享受一个模拟的window对象吧。
59+
60+
`test/my.react.test.js`:
61+
62+
```js
63+
import test from 'ava';
64+
import React from 'react';
65+
import {render} from 'react-dom';
66+
import {Simulate} from 'react-addons-test-utils';
67+
import sinon from 'sinon';
68+
import CustomInput from './components/custom-input.jsx';
69+
70+
test('Input calls onBlur', t => {
71+
const onUserBlur = sinon.spy();
72+
const input = render(
73+
React.createElement(CustomInput, {onUserBlur),
74+
div
75+
)
76+
77+
Simulate.blur(input);
78+
79+
t.true(onUserBlur.calledOnce);
80+
});
81+
```

zh_CN/docs/recipes/code-coverage.md

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
___
2+
**备注**
3+
4+
这是[code-coverage.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/code-coverage.md)的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/master...zhaozhiming:master)用来查看本翻译与AVA的master分支是否有差别(如果你没有看到`code-coverage`发生变化,那就意味着这份翻译文档是最新的)。
5+
___
6+
7+
# 代码覆盖率
8+
9+
翻译: [Español](https://github.com/sindresorhus/ava-docs/blob/master/es_ES/docs/recipes/code-coverage.md), [Français](https://github.com/sindresorhus/ava-docs/blob/master/fr_FR/docs/recipes/code-coverage.md), [日本語](https://github.com/sindresorhus/ava-docs/blob/master/ja_JP/docs/recipes/code-coverage.md), [Português](https://github.com/sindresorhus/ava-docs/blob/master/pt_BR/docs/recipes/code-coverage.md), [Русский](https://github.com/sindresorhus/ava-docs/blob/master/ru_RU/docs/recipes/code-coverage.md), [简体中文](https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/docs/recipes/code-coverage.md)
10+
11+
12+
因为AVA[重新处理了测试文件][process-isolation],所以你不能使用[`istanbul`]来做代码覆盖率,但你可以使用[`nyc`]来完成,它是支持子进程的[`istanbul`]
13+
14+
## 设置
15+
16+
首先安装NYC:
17+
18+
```
19+
$ npm install nyc --save-dev
20+
```
21+
22+
然后添加`.nyc_output``coverage`文件夹到你的`.gitignore`文件。
23+
24+
`.gitignore`:
25+
26+
```
27+
node_modules
28+
coverage
29+
.nyc_output
30+
```
31+
32+
## ES5覆盖率
33+
34+
使用NYC很简单就可以提供使用ES5来写的生产代码的覆盖率,只需要在测试脚本前面加上`nyc`
35+
36+
```json
37+
{
38+
"scripts": {
39+
"test": "nyc ava"
40+
}
41+
}
42+
```
43+
44+
就是这样!
45+
46+
如果你想要创建HTML覆盖率报告,或者上传覆盖率数据到Coveralls,你应该跳过下面的那些章节。
47+
48+
## ES2015覆盖率
49+
50+
使用Babel来转换生产代码有点复杂,这里我们把它分成几个步骤。
51+
52+
### 配置Babel
53+
54+
首先,我们需要一个Babel配置,下面是一个例子,你可以修改它以适应你的需要。
55+
56+
`package.json`:
57+
```json
58+
{
59+
"babel": {
60+
"presets": ["es2015"],
61+
"plugins": ["transform-runtime"],
62+
"ignore": "test.js",
63+
"env": {
64+
"development": {
65+
"sourceMaps": "inline"
66+
}
67+
}
68+
}
69+
}
70+
```
71+
72+
例子中这里有2点比较重要。
73+
74+
1. 我们忽略测试文件,因为AVA已经为你做了转换处理了。
75+
76+
2. 我们为开发环境指定`inline`(内联)原生映射,为了正确的生成覆盖率这个很重要,使用Babel配置中的`env`属性可以让我们在生产构建中取消原生映射。
77+
78+
79+
### 创建一个构建脚本
80+
81+
因为你可能不希望在生产代码中`inline`原生映射,你需要在构建脚本中指定一个可代替的环境变量:
82+
83+
`package.json`
84+
85+
```json
86+
{
87+
"scripts": {
88+
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
89+
}
90+
}
91+
```
92+
93+
> 警告:`BABEL_ENV=production`在Windows中不可用,你必须使用`set`关键字(`set BABEL_ENV=production`),如果是跨平台构建,请检查[`cross-env`]
94+
95+
注意,构建脚本中AVA的部分真的很少,它只是一个如何使用Babel的`env`配置来操作你的配置以兼容AVA的示例。
96+
97+
### 使用Babel require钩子
98+
99+
要使用Babel require钩子,请在`package.json`中的AVA配置里将`require`属性设置为`babel-core/register`
100+
101+
```json
102+
{
103+
"ava": {
104+
"require": ["babel-core/register"]
105+
}
106+
}
107+
```
108+
109+
*注意*:你也可以在命令行里设置require钩子:`ava --require=babel-core/register`。尽管如此,配置在`package.json`里面可以让你不用重复地写标志。
110+
111+
### 把所有东西放在一起
112+
113+
结合上面的步骤,你的`package.json`最后可能是这个样子:
114+
115+
```json
116+
{
117+
"scripts": {
118+
"test": "nyc ava",
119+
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
120+
},
121+
"babel": {
122+
"presets": ["es2015"],
123+
"plugins": ["transform-runtime"],
124+
"ignore": "test.js",
125+
"env": {
126+
"development": {
127+
"sourceMaps": "inline"
128+
}
129+
}
130+
},
131+
"ava": {
132+
"require": ["babel-core/register"]
133+
}
134+
}
135+
```
136+
137+
138+
## HTML报告
139+
140+
NYC创建在`.nyc_ouput`文件夹中为每个进程创建一个`json`的覆盖率文件。
141+
142+
把这些文件组合成一个可阅读的HTML报告,可以通过下面的方法来做:
143+
144+
```
145+
$ ./node_modules/.bin/nyc report --reporter=html
146+
```
147+
148+
或者,使用npm脚本来代替打印命令行:
149+
150+
```json
151+
{
152+
"scripts": {
153+
"report": "nyc report --reporter=html"
154+
}
155+
}
156+
```
157+
158+
这样会在`coverage`文件夹中输出一个HTML文件。
159+
160+
161+
## 托管覆盖率报告
162+
163+
### Travis CI & Coveralls
164+
165+
首先,你需要登录[coveralls.io]并激活你的项目库。
166+
167+
一旦完成,添加[`coveralls`]到开发依赖库:
168+
169+
```
170+
$ npm install coveralls --save-dev
171+
```
172+
173+
然后添加下面的代码到你的`.travis.yml`
174+
175+
```yaml
176+
after_success:
177+
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
178+
```
179+
180+
你的覆盖率报告将在你的Travis完成后很快地出现在coveralls上面。
181+
182+
[`babel`]: https://github.com/babel/babel
183+
[coveralls.io]: https://coveralls.io
184+
[`coveralls`]: https://github.com/nickmerwin/node-coveralls
185+
[`cross-env`]: https://github.com/kentcdodds/cross-env
186+
[process-isolation]: https://github.com/sindresorhus/ava#process-isolation
187+
[`istanbul`]: https://github.com/gotwarlost/istanbul
188+
[`nyc`]: https://github.com/bcoe/nyc

zh_CN/readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -903,11 +903,11 @@ AVA,不是Ava,也不是ava,发音[`/ˈeɪvə/` ay-və](https://github.com/
903903

904904
## 秘方
905905

906-
- [代码覆盖率](https://github.com/sindresorhus/ava/blob/master/docs/recipes/code-coverage.md)
906+
- [代码覆盖率](docs/recipes/code-coverage.md)
907907
- [观察模式](https://github.com/sindresorhus/ava/blob/master/docs/recipes/watch-mode.md)
908908
- [端到端测试](https://github.com/sindresorhus/ava/blob/master/docs/recipes/endpoint-testing.md)
909909
- [什么时候使用`t.plan()`](https://github.com/sindresorhus/ava/blob/master/docs/recipes/when-to-use-plan.md)
910-
- [浏览器测试](https://github.com/sindresorhus/ava/blob/master/docs/recipes/browser-testing.md)
910+
- [浏览器测试](docs/recipes/browser-testing.md)
911911
- [TypeScript](https://github.com/sindresorhus/ava/blob/master/docs/recipes/typescript.md)
912912

913913
## 支持

0 commit comments

Comments
 (0)