Skip to content

Commit 9fb6e7f

Browse files
committed
doc: add end-point testing and typescript translate
1 parent 6751d5e commit 9fb6e7f

File tree

5 files changed

+105
-4
lines changed

5 files changed

+105
-4
lines changed

zh_CN/docs/recipes/browser-testing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
___
22
**备注**
33

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`发生变化,那就意味着这份翻译文档是最新的)。
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.md`发生变化,那就意味着这份翻译文档是最新的)。
55
___
66

77
# 设置AVA做浏览器测试

zh_CN/docs/recipes/code-coverage.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
___
22
**备注**
33

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`发生变化,那就意味着这份翻译文档是最新的)。
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.md`发生变化,那就意味着这份翻译文档是最新的)。
55
___
66

77
# 代码覆盖率
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
___
2+
**备注**
3+
4+
这是[endpoint-testing.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/endpoint-testing.md)的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/master...zhaozhiming:master)用来查看本翻译与AVA的master分支是否有差别(如果你没有看到`endpoint-testing.md`发生变化,那就意味着这份翻译文档是最新的)。
5+
___
6+
7+
# 端点测试
8+
9+
翻译: [Español](https://github.com/sindresorhus/ava-docs/blob/master/es_ES/docs/recipes/endpoint-testing.md), [Français](https://github.com/sindresorhus/ava-docs/blob/master/fr_FR/docs/recipes/endpoint-testing.md), [日本語](https://github.com/sindresorhus/ava-docs/blob/master/ja_JP/docs/recipes/endpoint-testing.md), [Português](https://github.com/sindresorhus/ava-docs/blob/master/pt_BR/docs/recipes/endpoint-testing.md), [Русский](https://github.com/sindresorhus/ava-docs/blob/master/ru_RU/docs/recipes/endpoint-testing.md), [简体中文](https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/docs/recipes/endpoint-testing.md)
10+
11+
AVA没有内嵌的方法可以来做端点测试,但你可以用其他断言库来做,让我们用[`supertest-as-promised`](https://github.com/WhoopInc/supertest-as-promised)来看看。
12+
13+
因为测试是并发执行的,所以最好是为每个测试建立一个新的服务器实例,如果所有测试都引用同一个实例,那实例可能会被不同的测试改变状态。这可以在`test.beforeEach``t.context`里完成,或者简单的工厂方法:
14+
15+
```js
16+
function makeApp() {
17+
const app = express();
18+
app.post('/signup', signupHandler);
19+
return app;
20+
}
21+
```
22+
23+
然后,将你的服务器注入到测试超类中,主要的点是用promise或async/await语法来代替测试超类的`end`方法:
24+
25+
```js
26+
test('signup:Success', async t => {
27+
t.plan(2);
28+
29+
const res = await request(makeApp())
30+
.post('/signup')
31+
.send({email: '[email protected]', password: '123123'});
32+
33+
t.is(res.status, 200);
34+
t.is(res.body.email, '[email protected]');
35+
});
36+
```
37+

zh_CN/docs/recipes/typescript.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
___
2+
**备注**
3+
4+
这是[typescript.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/typescript.md)的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/master...zhaozhiming:master)用来查看本翻译与AVA的master分支是否有差别(如果你没有看到`typescript.md`发生变化,那就意味着这份翻译文档是最新的)。
5+
___
6+
7+
# TypeScript
8+
9+
翻译: [Français](https://github.com/sindresorhus/ava-docs/blob/master/fr_FR/docs/recipes/typescript.md), [Русский](https://github.com/sindresorhus/ava-docs/blob/master/ru_RU/docs/recipes/typescript.md), [简体中文](https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/docs/recipes/typescript.md)
10+
11+
AVA捆绑了一个TypeScript定义文件,让开发人员可以了解如何用TypeScript写测试。
12+
13+
## 设置
14+
15+
首先安装TypeScript编译器[tsc](https://github.com/Microsoft/TypeScript)
16+
17+
```
18+
$ npm install --save-dev tsc
19+
```
20+
21+
创建一个[`tsconfig.json`](https://github.com/Microsoft/TypeScript/wiki/tsconfig.json)文件,文件指定编译器是用来编译工程或者测试文件。
22+
23+
```json
24+
{
25+
"compilerOptions": {
26+
"module": "commonjs",
27+
"target": "es2015"
28+
}
29+
}
30+
```
31+
32+
`package.json`文件里添加一个`test`脚本,在运行AVA前先编译工程。
33+
34+
```json
35+
{
36+
"scripts": {
37+
"test": "tsc && ava"
38+
}
39+
}
40+
```
41+
42+
43+
## 添加测试
44+
45+
创建一个`test.ts`文件。
46+
47+
```ts
48+
import test from 'ava';
49+
50+
async function fn() {
51+
return Promise.resolve('foo');
52+
}
53+
54+
test(async (t) => {
55+
t.is(await fn(), 'foo');
56+
});
57+
```
58+
59+
60+
## 执行测试
61+
62+
```
63+
$ npm test
64+
```

zh_CN/readme.md

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

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

913913
## 支持
914914

0 commit comments

Comments
 (0)