Skip to content

Commit 176dc44

Browse files
committed
doc: add watch-mode and when to use plan translate
1 parent 9fb6e7f commit 176dc44

File tree

3 files changed

+249
-2
lines changed

3 files changed

+249
-2
lines changed

zh_CN/docs/recipes/watch-mode.md

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
___
2+
**备注**
3+
4+
这是[watch-mode.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/watch-mode.md)的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/master...zhaozhiming:master)用来查看本翻译与AVA的master分支是否有差别(如果你没有看到`watch-mode.md`发生变化,那就意味着这份翻译文档是最新的)。
5+
___
6+
7+
# 观察模式
8+
9+
翻译: [Français](https://github.com/sindresorhus/ava-docs/blob/master/fr_FR/docs/recipes/watch-mode.md), [Русский](https://github.com/sindresorhus/ava-docs/blob/master/ru_RU/docs/recipes/watch-mode.md), [简体中文](https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/docs/recipes/watch-mode.md)
10+
11+
12+
AVA自带了一个聪明的观察模式,它会观察那些改变了的文件并运行受到改变影响的测试。
13+
14+
## 运行测试时启用观察模式
15+
16+
你可以通过使用`--watch``-w`标志来启用观察模式,如果你是全局安装的AVA:
17+
18+
```console
19+
$ ava --watch
20+
```
21+
22+
如果你将AVA配置在`package.json`中,像这样:
23+
24+
```json
25+
{
26+
"scripts": {
27+
"test": "ava"
28+
}
29+
}
30+
```
31+
32+
你可以这样运行:
33+
34+
```console
35+
$ npm test -- --watch
36+
```
37+
38+
你可以设置一个特殊的脚本:
39+
40+
```json
41+
{
42+
"scripts": {
43+
"test": "ava",
44+
"test:watch": "ava --watch"
45+
}
46+
}
47+
```
48+
49+
然后使用:
50+
51+
```console
52+
$ npm run test:watch
53+
```
54+
55+
## 要求
56+
57+
AVA使用[`chokidar`]来作为文件观察期,它被配置为可选的依赖,因为`chokidar`有时候无法安装,如果`chokidar`安装失败那么观察模式就不可用,然后你将看到一条这样的信息:
58+
59+
> The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.
60+
61+
请参考[`chokidar`文档][`chokidar`]了解如何解决这个问题。
62+
63+
## 源文件和测试文件
64+
65+
在AVA中*源文件**测试文件*是有差别的,正如你所想的一样,*测试文件*包含了你的测试,*源文件*是需要支持测试运行的
66+
其他所有文件,是你的源代码或者测试数据。
67+
68+
默认情况下AVA观察测试文件,`package.json`和其他的`.js`文件的改变,它会忽略由[`ignore-by-default`]包提供的[特定文件夹](https://github.com/novemberborn/ignore-by-default/blob/master/index.js)下的文件。
69+
70+
你可以使用[`--source` CLI 标志]`package.json`文件的`ava`属性为源文件配置模式,注意如果你从[`ignore-by-default`]中指定了一个负模式目录,那么忽略将不再有效,所以你可能想要在你的配置里重复这些操作。
71+
72+
如果你的测试会写入磁盘,那么它们会跟踪观察器来返回你的测试,这种情况下你需要使用`--source`标志。
73+
74+
## 依赖跟踪
75+
76+
AVA跟踪测试文件依赖的源文件,如果你改变的源文件只有一个测试依赖那么久只会返回这个测试,如果它不能识别哪个测试文件依赖了这个被修改的源文件,那么它会返回所有的测试。
77+
78+
依赖跟踪在required模式中有效,支持自定义继承和转换,使用[`--require` CLI标志]而不是从你的测试文件来帮助你加载它们。使用`fs`模块来做的文件访问不会被跟踪。
79+
80+
## 手动返回所有测试
81+
82+
你可以在console中,通过在<kbd>Enter</kbd>后面打印<kbd>r</kbd>来快速返回所有测试,
83+
84+
## 调试
85+
86+
有时候观察模式会出现一些意想不到的事情,比如当你只运行一个测试时会返回所有测试,为了调查原因你可以启用调试模式:
87+
88+
```console
89+
$ DEBUG=ava:watcher npm test -- --watch
90+
```
91+
92+
在Windows里这样用:
93+
94+
```console
95+
$ set DEBUG=ava:watcher
96+
$ npm test -- --watch
97+
```
98+
99+
## 帮助我们改善观察模式
100+
101+
观察模式比较新并且现在处于初期阶段,请[报告](https://github.com/sindresorhus/ava/issues)任何你遇到问题,谢谢!
102+
103+
[`chokidar`]: https://github.com/paulmillr/chokidar
104+
[`ignore-by-default`]: https://github.com/novemberborn/ignore-by-default
105+
[`--require` CLI 标志]: https://github.com/sindresorhus/ava#cli
106+
[`--source` CLI 标志]: https://github.com/sindresorhus/ava#cli
107+
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
___
2+
**备注**
3+
4+
这是[when-to-use-plan.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/when-to-use-plan.md)的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/master...zhaozhiming:master)用来查看本翻译与AVA的master分支是否有差别(如果你没有看到`when-to-use-plan.md`发生变化,那就意味着这份翻译文档是最新的)。
5+
___
6+
7+
# 什么时候使用`t.plan()`
8+
9+
翻译: [Español](https://github.com/sindresorhus/ava-docs/blob/master/es_ES/docs/recipes/when-to-use-plan.md), [Français](https://github.com/sindresorhus/ava-docs/blob/master/fr_FR/docs/recipes/when-to-use-plan.md), [日本語](https://github.com/sindresorhus/ava-docs/blob/master/ja_JP/docs/recipes/when-to-use-plan.md), [Português](https://github.com/sindresorhus/ava-docs/blob/master/pt_BR/docs/recipes/when-to-use-plan.md), [Русский](https://github.com/sindresorhus/ava-docs/blob/master/ru_RU/docs/recipes/when-to-use-plan.md), [简体中文](https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/docs/recipes/when-to-use-plan.md)
10+
11+
AVA和[`tap`](https://github.com/tapjs/node-tap)/[`tape`](https://github.com/substack/tape)的一个主要不同是`t.plan()`的行为。在AVA中`t.plan()`只是用来断言期望的断言数是否正确,并不会自动结束测试。
12+
13+
## `t.plan()`不好的用法
14+
15+
很多从`tap/tape`过渡过来的用户习惯在每个测试里大量使用`t.plan()`,尽管如此,在AVA里我们不认为这是一个“最佳实践”,我们反而认为`t.plan()`只有在少数情况才能提供其价值。
16+
17+
### 没有分支的同步测试
18+
19+
在大部分同步测试中`t.plan()`是没有必要的。
20+
21+
```js
22+
test(t => {
23+
// 不好:这里没有分支,t.plan是无意义的
24+
t.plan(2);
25+
26+
t.is(1 + 1, 2);
27+
t.is(2 + 2, 4);
28+
});
29+
```
30+
31+
`t.plan()`在这里并没有提供任何价值,并且如果你决定添加或删除断言时会带来额外的工作。
32+
33+
### Promises期望得到resolve
34+
35+
```js
36+
test(t => {
37+
t.plan(1);
38+
39+
return somePromise().then(result => {
40+
t.is(result, 'foo');
41+
});
42+
});
43+
```
44+
45+
简单看一下,这个测试充分利用了`t.plan()`的优势,因为这里面涉及到了异步promise处理。尽管如此在这个测试里还有一些问题:
46+
47+
1. `t.plan()`用在这里大概是为了防止`somePromise()`可能被reject的情况,但返回一个reject的promise测试始终会失败。
48+
49+
2. 使用`async`/`await`可能会更好。
50+
51+
```js
52+
test(async t => {
53+
t.is(await somePromise(), 'foo');
54+
});
55+
```
56+
57+
## `t.plan()`好的用法
58+
59+
`t.plan()`有很多可接受的用法。
60+
61+
### Promise带上一个`.catch()`
62+
63+
```js
64+
test(t => {
65+
t.plan(2);
66+
67+
return shouldRejectWithFoo().catch(reason => {
68+
t.is(reason.message, 'Hello') // 如果你关心的是message的话使用t.throws()更好
69+
t.is(reason.foo, 'bar');
70+
});
71+
});
72+
```
73+
74+
在这里,`t.plan()`用来确保`catch`块中的代码被执行,在大部分情况下,你更应该使用`t.throws()`断言,但这是一个可以接受的用法,因为`t.throws()`只允许你断言错误的`message`属性。
75+
76+
### 确保一个catch语句发生
77+
78+
```js
79+
test(t => {
80+
t.plan(2);
81+
82+
try {
83+
shouldThrow();
84+
} catch (err) {
85+
t.is(err.message, 'Hello') // 如果你关心的是message的话使用t.throws()更好
86+
t.is(err.foo, 'bar');
87+
}
88+
});
89+
```
90+
91+
像上面这种`try`/`catch`的情况,使用`t.throws()`一般是个更好的选择,但它只允许你断言错误的`message`属性。
92+
93+
### 确保多个callback被真正调用
94+
95+
```js
96+
test.cb(t => {
97+
t.plan(2);
98+
99+
const callbackA = () => {
100+
t.pass();
101+
t.end();
102+
};
103+
104+
const callbackB = () => t.pass();
105+
106+
bThenA(callbackA, callbackB);
107+
});
108+
```
109+
110+
上面确保`callbackB`先被调用(且只调用一次),紧接着调用`callbackA`,其他的组合不会满足计划。
111+
112+
### 测试带有分支语句
113+
114+
在大部分情况下,在测试中使用复杂的分支是一个坏主意,一个明显的例外是用来测试自动生成的东西(可能来自一个JSON文档)。下面的`t.plan()`用来确保JSON输入的正确性:
115+
116+
```js
117+
const testData = require('./fixtures/test-definitions.json');
118+
119+
testData.forEach(testDefinition => {
120+
test(t => {
121+
const result = functionUnderTest(testDefinition.input);
122+
123+
// testDefinition应该有一个`foo`或`bar`的预期而不是同时满足它们
124+
t.plan(1);
125+
126+
if (testDefinition.foo) {
127+
t.is(result.foo, testDefinition.foo);
128+
}
129+
130+
if (testDefinition.bar) {
131+
t.is(result.bar, testDefinition.foo);
132+
}
133+
});
134+
});
135+
```
136+
137+
## 总结
138+
139+
`t.plan()`有很多有效的使用方法,但不应该被盲目使用。一个好的经验法则是你的*测试*没有简单的,容易推断的,代码流你可以使用它。测试在callback里带有断言,`if`/`then`语句,`for`/`while`循环,并且(在一些情况下)`try`/`catch`块,都可以考虑使用`t.plan()`
140+

zh_CN/readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,9 @@ AVA,不是Ava,也不是ava,发音[`/ˈeɪvə/` ay-və](https://github.com/
904904
## 秘方
905905

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

0 commit comments

Comments
 (0)