diff --git a/readme.md b/readme.md index 4e2a8732..90bc8b8c 100644 --- a/readme.md +++ b/readme.md @@ -5,6 +5,7 @@ - [日本語](ja_JP/readme.md) *(by [@makotot](https://github.com/makotot))* - [Português](pt_BR/readme.md) *(by [@charbelrami](https://github.com/charbelrami))* - [Русский](ru_RU/readme.md) *(by [@sohje](https://github.com/sohje))* +- [简体中文](zh_CN/readme.md) *(by [@zhaozhiming](https://github.com/zhaozhiming))* ## Contribute diff --git a/zh_CN/code-of-conduct.md b/zh_CN/code-of-conduct.md new file mode 100644 index 00000000..41795985 --- /dev/null +++ b/zh_CN/code-of-conduct.md @@ -0,0 +1,37 @@ +___ +**备注** + +这是 [code-of-coduct.md](https://github.com/sindresorhus/ava/blob/master/code-of-coduct.md) 的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/89767ec3b6174e59d37faaadb50cfa3c0d58bda6...master#diff-d3030a18b089fdb1fbfabf6e75e4aef0) 用来查看本翻译与 AVA 的 master 分支是否有差别(如果你没有看到`code-of-coduct.md`发生变化,那就意味着这份翻译文档是最新的)。 +___ + +# 贡献者的行为准则 + +翻译:[Español](https://github.com/sindresorhus/ava-docs/blob/master/es_ES/code-of-conduct.md), [Français](https://github.com/sindresorhus/ava-docs/blob/master/fr_FR/code-of-conduct.md), [日本語](https://github.com/sindresorhus/ava-docs/blob/master/ja_JP/code-of-conduct.md), [Português](https://github.com/sindresorhus/ava-docs/blob/master/pt_BR/code-of-conduct.md), [Русский](https://github.com/sindresorhus/ava-docs/blob/master/ru_RU/code-of-conduct.md), [简体中文](https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/code-of-conduct.md) + +作为这个项目的贡献者和维护者,我们希望社区开放和受欢迎,我们承诺尊重所有参与项目的人,不管是报告问题,提交功能需求,更新文档,提交 pull request 或 patch,或者是其他积极的活动。 + +我们致力于让每个参与项目的人享受相同的体验,不管经验水平,性别,性别身份和表达,性取向,残疾,个人形象,身体大小,种族,种族渊源,年龄,宗教信仰或者国籍。 + +参与者不被接收的行为有如下例子: + +* 色情内容或图片 +* 个人攻击 +* 恶意攻击或者侮辱、贬低他人的言论 +* 公开或者私下骚扰 +* 公布他人私人信息,包括现实和网络的信息 +* 没有得到明确许可进行的演讲 +* 其他不道德和不专业的行为 + +项目维护者有如下权利和义务,包括删除,编辑或者拒绝评论,提交,代码,wiki 编辑,问题和其他贡献者没有遵守行为准则的行为等操作,可以临时或永久的禁止任何贡献者,如果认为贡献者不合适,威胁他人,无礼或者有害的,会进行临时或者永久禁止的处理。 + +通过采用这份行为准则,项目维护者承诺在项目管理的方方面面上公平和坚持地贯彻这些原则。如果有维护者没有遵守或执行行为准则,他可能就会在项目团队中被永久除名。 + + +当个人代表项目或者社区的时候,这份行为准则适用于项目空间和公共空间。 + +举报辱骂,骚扰或者其他不被接受的行为可以通过 sindresorhus@gmail.com 联系项目维护人员。所有的投诉都会被审计和调查,最终会产生一个认为必要和合理的结果。维护人员有义务为报告者保密其信息。 + +这份行为准则改编自[贡献者契约][homepage],版本 1.3.0,可在 [http://contributor-covenant.org/version/1/3/0/][version] 获取。 + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/3/0/ diff --git a/zh_CN/contributing.md b/zh_CN/contributing.md new file mode 100644 index 00000000..1ace24c7 --- /dev/null +++ b/zh_CN/contributing.md @@ -0,0 +1,52 @@ +___ +**备注** + +这是 [contributing.md](https://github.com/sindresorhus/ava/blob/master/contributing.md) 的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/89767ec3b6174e59d37faaadb50cfa3c0d58bda6...master#diff-cc4aac3e9be04e0413c9520f223b493c) 用来查看本翻译与 AVA 的 master 分支是否有差别(如果你没有看到`contributing.md`发生变化,那就意味着这份翻译文档是最新的)。 +___ +# 向 AVA 贡献 + +✨ 感谢向 AVA 作出贡献! ✨ + +请注意,这个项目发布带有[贡献者的行为准则](code-of-conduct.md),参与这个项目你需要同意并遵守其中的条款。 + +翻译:[Español](https://github.com/sindresorhus/ava-docs/blob/master/es_ES/contributing.md), [Français](https://github.com/sindresorhus/ava-docs/blob/master/fr_FR/contributing.md), [日本語](https://github.com/sindresorhus/ava-docs/blob/master/ja_JP/contributing.md), [Português](https://github.com/sindresorhus/ava-docs/blob/master/pt_BR/contributing.md), [Русский](https://github.com/sindresorhus/ava-docs/blob/master/ru_RU/contributing.md), [简体中文](https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/contributing.md) + +## 我怎么贡献? + +### 改进文档 + +作为 AVA 的用户你是帮助我们改进文档的最佳候选人,修改拼写错误,修复错误,更好的解释,更多的例子等等。为一些可以改进的事情提问题,[帮助我们翻译文档](https://github.com/sindresorhus/ava-docs),不管任何事情,即使是改善现在这个文档。 + +### 改善问题 + +一些问题在创建时缺少信息,不能重现,描述太过简单无效,帮助我们将他们变得更容易处理,因为处理问题需要大量时间,我们情愿将时间花在修复缺陷和添加新功能上面。 + +### 在问题中给出反馈 + +我们总是在问题跟踪器上寻找更多的讨论意见,这是一个影响 AVA 未来发展方向的好机会。 + +### 在我们的聊天室中闲聊 + +我们有一个[聊天室](https://gitter.im/sindresorhus/ava),可以进到里面潜水,跟我们聊天,或者帮助其他人。 + +### 提交问题 + +- 问题跟踪器是针对问题的,请使用我们的[聊天室](https://gitter.im/sindresorhus/ava) 或者 [Stack Overflow](https://stackoverflow.com/questions/tagged/ava) 来寻求支持。 +- 在新开一个问题之前先搜索以前的问题。 +- 确保你使用的是最新版本的 AVA。 +- 用一个清晰和描述性好的标题。 +- 包含尽可能多的信息:重现问题的步骤,错误的信息,Node.js 的版本,操作系统等。 +- 在问题的描述上你花越多时间,越多的信息将给到我们。 +- [最好的问题提交方式是提供一个失败的测试案例。](https://twitter.com/sindresorhus/status/579306280495357953) + +### 提交一个 pull rquest + +- 重大的修改最好是先开一个问题来进行讨论,这样可以避免你做一些不必要的工作。 +- 对于长期远大的任务,你应该将你所做的工作在社区中提出来并尽快得到反馈,尽快开一个能证明你想法的最简版本的 pull request。在前期,不需要把事情做得完美,或者 100% 完成,只需要在标题添加一个 [WIP] 前缀,然后描述哪些工作你需要继续做的。这样评审人员就不会挑剔其中的小细节或者指出哪些你已经知道的改进点。 +- 新功能应该具备测试和文档。 +- 不要包含不相关的修改。 +- 在提交 pull request 之前检查代码和运行测试,通过执行`$ npm test`命令来完成。 +- 在一个[主题分支](https://github.com/dchelimsky/rspec/wiki/Topic-Branches) 中提交 pull request 而不是 master 分支。 +- 为 pull request 和 commit 使用一个清晰和描述性强的标题。 +- 写一个让人信服的描述来说明为什么我们要接受你的 pull request。说服我们是你的工作,需要回答“为什么”并提供用例。 +- 你可能被要求修改你的 pull request,但绝对没有必要去新开一个 pull request,[只需要更新原来那个就可以了。](https://github.com/RichardLitt/docs/blob/master/amending-a-commit-guide.md) diff --git a/zh_CN/docs/recipes/browser-testing.md b/zh_CN/docs/recipes/browser-testing.md new file mode 100644 index 00000000..404464ed --- /dev/null +++ b/zh_CN/docs/recipes/browser-testing.md @@ -0,0 +1,81 @@ +___ +**备注** + +这是 [browser-testing.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/browser-testing.md) 的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/8e2f3dca177a4283ad882596d3c1425cabb998ef...master#diff-9d3d394077fa7f97cbbb0fefc098ac60) 用来查看本翻译与 AVA 的 master 分支是否有差别(如果你没有看到`browser-testing.md`发生变化,那就意味着这份翻译文档是最新的)。 +___ + +# 设置 AVA 做浏览器测试 + +翻译:[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) + + + +AVA[还](https://github.com/sindresorhus/ava/issues/24) 不支持在浏览器中运行测试。一些库要求浏览器指定全局变量(`window`, `document`, `navigator`等等)。 +React 就是其中的一个例子,最低要求如果你想用 ReactDOM.render 和用 ReactTestUTils 模拟事件。 + +这个秘方让需要模拟浏览器环境的库可以工作。 + +## 安装 jsdom + +安装 [jsdom](https://github.com/tmpvar/jsdom)。 + +> 一个 WHATWG DOM 和 HTML 标准的 JavaScript 实现,给 node.js 使用的。 + +``` +$ npm install --save-dev jsdom +``` + +## 设置 jsdom + +创建一个 helper 文件并放在`test/helpers`文件夹中,这样确保 AVA 不会把它当成测试来处理。 + +`test/helpers/setup-browser-env.js`: + +```js +global.document = require('jsdom').jsdom('
'); +global.window = document.defaultView; +global.navigator = window.navigator; +``` + +## 配置测试使用 jsdom + +配置 AVA,将`require`设置为 helper 文件,这样每个测试运行前都会先加载它。 + +`package.json`: + +```json +{ + "ava": { + "require": [ + "./test/helpers/setup-browser-env.js" + ] + } +} +``` + +## 享受! + +编写你的测试并享受一个模拟的 window 对象吧。 + +`test/my.react.test.js`: + +```js +import test from 'ava'; +import React from 'react'; +import {render} from 'react-dom'; +import {Simulate} from 'react-addons-test-utils'; +import sinon from 'sinon'; +import CustomInput from './components/custom-input.jsx'; + +test('Input calls onBlur', t => { + const onUserBlur = sinon.spy(); + const input = render( + React.createElement(CustomInput, {onUserBlur), + div + ) + + Simulate.blur(input); + + t.true(onUserBlur.calledOnce); +}); +``` diff --git a/zh_CN/docs/recipes/code-coverage.md b/zh_CN/docs/recipes/code-coverage.md new file mode 100644 index 00000000..561a249f --- /dev/null +++ b/zh_CN/docs/recipes/code-coverage.md @@ -0,0 +1,188 @@ +___ +**备注** + +这是 [code-coverage.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/code-coverage.md) 的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/1868204c1901f45b4f66a520ef6486fdd71fe1d2...master#diff-b3aa0c81a407f54f636a1cf5a619a4a6) 用来查看本翻译与 AVA 的 master 分支是否有差别(如果你没有看到`code-coverage.md`发生变化,那就意味着这份翻译文档是最新的)。 +___ + +# 代码覆盖率 + +翻译:[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) + + +因为 AVA[重新处理了测试文件][process-isolation],所以你不能使用 [`istanbul`] 来做代码覆盖率,但你可以使用 [`nyc`] 来完成,它是支持子进程的 [`istanbul`]。 + +## 设置 + +首先安装 NYC: + +``` +$ npm install nyc --save-dev +``` + +然后添加`.nyc_output`和`coverage`文件夹到你的`.gitignore`文件。 + +`.gitignore`: + +``` +node_modules +coverage +.nyc_output +``` + +## ES5 覆盖率 + +使用 NYC 很简单就可以提供使用 ES5 来写的生产代码的覆盖率,只需要在测试脚本前面加上`nyc`: + +```json +{ + "scripts": { + "test": "nyc ava" + } +} +``` + +就是这样! + +如果你想要创建 HTML 覆盖率报告,或者上传覆盖率数据到 Coveralls,你应该跳过下面的那些章节。 + +## ES2015 覆盖率 + +使用 Babel 来转换生产代码有点复杂,这里我们把它分成几个步骤。 + +### 配置 Babel + +首先,我们需要一个 Babel 配置,下面是一个例子,你可以修改它以适应你的需要。 + +`package.json`: +```json +{ + "babel": { + "presets": ["es2015"], + "plugins": ["transform-runtime"], + "ignore": "test.js", + "env": { + "development": { + "sourceMaps": "inline" + } + } + } +} +``` + +例子中有 2 点比较重要: + +1. 我们忽略测试文件,因为 AVA 已经为你做了转换处理了。 + +2. 我们为开发环境指定`inline`(内联)原生映射,为了正确的生成覆盖率这个很重要,使用 Babel 配置中的`env`属性可以让我们在生产构建中取消原生映射。 + + +### 创建一个构建脚本 + +因为你可能不希望在生产代码中`inline`原生映射,你需要在构建脚本中指定一个可代替的环境变量: + +`package.json` + +```json +{ + "scripts": { + "build": "BABEL_ENV=production babel --out-dir=dist index.js" + } +} +``` + +> 警告:`BABEL_ENV=production`在 Windows 中不可用,你必须使用`set`关键字(`set BABEL_ENV=production`),如果是跨平台构建,请检查 [`cross-env`]。 + +注意,构建脚本中 AVA 的部分真的很少,它只是一个如何使用 Babel 的`env`配置来操作你的配置以兼容 AVA 的示例。 + +### 使用 Babel require 钩子 + +要使用 Babel require 钩子,请在`package.json`中的 AVA 配置里将`require`属性设置为`babel-core/register`。 + +```json +{ + "ava": { + "require": ["babel-core/register"] + } +} +``` + +*注意*:你也可以在命令行里设置 require 钩子:`ava --require=babel-core/register`。尽管如此,配置在`package.json`里面可以让你不用重复地写标志。 + +### 把所有东西放在一起 + +结合上面的步骤,你的`package.json`最后可能是这个样子: + +```json +{ + "scripts": { + "test": "nyc ava", + "build": "BABEL_ENV=production babel --out-dir=dist index.js" + }, + "babel": { + "presets": ["es2015"], + "plugins": ["transform-runtime"], + "ignore": "test.js", + "env": { + "development": { + "sourceMaps": "inline" + } + } + }, + "ava": { + "require": ["babel-core/register"] + } +} +``` + + +## HTML 报告 + +NYC 在`.nyc_ouput`文件夹中为每个进程创建一个`json`的覆盖率文件。 + +把这些文件组合成一个可阅读的 HTML 报告,可以通过下面的方法来做: + +``` +$ ./node_modules/.bin/nyc report --reporter=html +``` + +或者,使用 npm 脚本来代替打印命令行: + +```json +{ + "scripts": { + "report": "nyc report --reporter=html" + } +} +``` + +这样会在`coverage`文件夹中输出一个 HTML 文件。 + + +## 托管覆盖率报告 + +### Travis CI & Coveralls + +首先,你需要登录 [coveralls.io] 并激活你的项目库。 + +一旦完成,添加 [`coveralls`] 到开发依赖库: + +``` +$ npm install coveralls --save-dev +``` + +然后添加下面的代码到你的`.travis.yml`: + +```yaml +after_success: + - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' +``` + +你的覆盖率报告将在你的 Travis 完成后很快地出现在 coveralls 上面。 + +[`babel`]: https://github.com/babel/babel +[coveralls.io]: https://coveralls.io +[`coveralls`]: https://github.com/nickmerwin/node-coveralls +[`cross-env`]: https://github.com/kentcdodds/cross-env +[process-isolation]: https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/readme.md#隔离进程 +[`istanbul`]: https://github.com/gotwarlost/istanbul +[`nyc`]: https://github.com/bcoe/nyc diff --git a/zh_CN/docs/recipes/endpoint-testing.md b/zh_CN/docs/recipes/endpoint-testing.md new file mode 100644 index 00000000..4c342c46 --- /dev/null +++ b/zh_CN/docs/recipes/endpoint-testing.md @@ -0,0 +1,36 @@ +___ +**备注** + +这是 [endpoint-testing.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/endpoint-testing.md) 的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/89767ec3b6174e59d37faaadb50cfa3c0d58bda6...master#diff-aee54ab6a703c02779edb3ebbb35e96f) 用来查看本翻译与 AVA 的 master 分支是否有差别(如果你没有看到`endpoint-testing.md`发生变化,那就意味着这份翻译文档是最新的)。 +___ + +# 端点测试 + +翻译:[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) + +AVA 没有内嵌的方法可以来做端点测试,但你可以用其他断言库来做,让我们用 [`supertest-as-promised`](https://github.com/WhoopInc/supertest-as-promised) 来看看。 + +因为测试是并发执行的,所以最好是为每个测试建立一个新的服务器实例,如果所有测试都引用同一个实例,那实例可能会被不同的测试改变状态。这可以在`test.beforeEach`和`t.context`里完成,或者简单的工厂方法: + +```js +function makeApp() { + const app = express(); + app.post('/signup', signupHandler); + return app; +} +``` + +然后,将你的服务器注入到测试超类中,主要的点是用 promise 或 async/await 语法来代替测试超类的`end`方法: + +```js +test('signup:Success', async t => { + t.plan(2); + + const res = await request(makeApp()) + .post('/signup') + .send({email: 'ava@rocks.com', password: '123123'}); + + t.is(res.status, 200); + t.is(res.body.email, 'ava@rocks.com'); +}); +``` diff --git a/zh_CN/docs/recipes/typescript.md b/zh_CN/docs/recipes/typescript.md new file mode 100644 index 00000000..eb25aeed --- /dev/null +++ b/zh_CN/docs/recipes/typescript.md @@ -0,0 +1,64 @@ +___ +**备注** + +这是 [typescript.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/typescript.md) 的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/8e2f3dca177a4283ad882596d3c1425cabb998ef...master#diff-60cce07a584082115d230f2e3d571ad6) 用来查看本翻译与 AVA 的 master 分支是否有差别(如果你没有看到`typescript.md`发生变化,那就意味着这份翻译文档是最新的)。 +___ + +# TypeScript + +翻译:[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) + +AVA 捆绑了一个 TypeScript 定义文件,让开发人员可以了解如何用 TypeScript 写测试。 + +## 设置 + +首先安装 TypeScript 编译器 [tsc](https://github.com/Microsoft/TypeScript)。 + +``` +$ npm install --save-dev tsc +``` + +创建一个 [`tsconfig.json`](https://github.com/Microsoft/TypeScript/wiki/tsconfig.json) 文件,文件指定编译器是用来编译工程或者测试文件。 + +```json +{ + "compilerOptions": { + "module": "commonjs", + "target": "es2015" + } +} +``` + +在`package.json`文件里添加一个`test`脚本,在运行 AVA 前先编译工程。 + +```json +{ + "scripts": { + "test": "tsc && ava" + } +} +``` + + +## 添加测试 + +创建一个`test.ts`文件。 + +```ts +import test from 'ava'; + +async function fn() { + return Promise.resolve('foo'); +} + +test(async (t) => { + t.is(await fn(), 'foo'); +}); +``` + + +## 执行测试 + +``` +$ npm test +``` diff --git a/zh_CN/docs/recipes/watch-mode.md b/zh_CN/docs/recipes/watch-mode.md new file mode 100644 index 00000000..ab7f8684 --- /dev/null +++ b/zh_CN/docs/recipes/watch-mode.md @@ -0,0 +1,105 @@ +___ +**备注** + +这是 [watch-mode.md](https://github.com/sindresorhus/ava/blob/master/docs/recipes/watch-mode.md) 的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/82c02bce80696547db0387dec243ddb470c8bce7...master#diff-92da4f3d087d796fdf4a45be88586b62) 用来查看本翻译与 AVA 的 master 分支是否有差别(如果你没有看到`watch-mode.md`发生变化,那就意味着这份翻译文档是最新的)。 +___ + +# 观察模式 + +翻译:[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) + + +AVA 自带了一个聪明的观察模式,它会观察那些改变了的文件并运行受到改变影响的测试。 + +## 运行测试时启用观察模式 + +你可以通过使用`--watch`或`-w`标志来启用观察模式,如果你是全局安装的 AVA: + +```console +$ ava --watch +``` + +如果你将 AVA 配置在`package.json`中,像这样: + +```json +{ + "scripts": { + "test": "ava" + } +} +``` + +你可以这样运行: + +```console +$ npm test -- --watch +``` + +你可以设置一个特殊的脚本: + +```json +{ + "scripts": { + "test": "ava", + "test:watch": "ava --watch" + } +} +``` + +然后使用: + +```console +$ npm run test:watch +``` + +## 要求 + +AVA 使用 [`chokidar`] 来作为文件观察器,它被配置为可选的依赖库,因为`chokidar`有时候无法安装,如果`chokidar`安装失败那么观察模式就不可用,然后你将看到一条这样的信息: + +> The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform. + +请参考 [`chokidar`文档][`chokidar`] 了解如何解决这个问题。 + +## 源文件和测试文件 + +在 AVA 中*源文件*和*测试文件*是有差别的,正如你所想的一样,*测试文件*包含了你的测试,*源文件*是需要支持测试运行的其他所有文件,是你的源代码或者测试数据。 + +默认情况下 AVA 观察测试文件,`package.json`和其他的`.js`文件的改变,它会忽略由 [`ignore-by-default`] 包提供的[特定文件夹](https://github.com/novemberborn/ignore-by-default/blob/master/index.js) 下的文件。 + +你可以使用 [`--source` CLI 标志』或`package.json`文件的`ava`属性为源文件配置模式,注意如果你从 [`ignore-by-default`] 中指定了一个负模式目录,那么忽略将不再有效,所以你可能想要在你的配置里重复这些操作。 + +如果你的测试会写入磁盘,那么它们会跟踪观察器来返回你的测试,这种情况下你需要使用`--source`标志。 + +## 依赖跟踪 + +AVA 跟踪测试文件依赖的源文件,如果你改变的源文件只有一个测试被依赖,那么就只会返回这个测试,如果它不能识别哪个测试文件依赖了这个被修改的源文件,那么它会返回所有的测试。 + +依赖跟踪在 required 模式中有效,支持自定义继承和转换,使用 [`--require` CLI 标志』而不是从你的测试文件来帮助你加载它们。使用`fs`模块来访问的文件不会被跟踪。 + +## 手动返回所有测试 + +你可以在 console 中,通过在Enter后面打印r来快速返回所有测试, + +## 调试 + +有时候观察模式会出现一些意想不到的事情,比如当你只运行一个测试时会返回所有测试,为了调查原因你可以启用调试模式: + +```console +$ DEBUG=ava:watcher npm test -- --watch +``` + +在 Windows 里这样用: + +```console +$ set DEBUG=ava:watcher +$ npm test -- --watch +``` + +## 帮助我们改善观察模式 + +观察模式比较新并且现在处于初期阶段,请[报告](https://github.com/sindresorhus/ava/issues) 任何你遇到问题,谢谢! + +[`chokidar`]: https://github.com/paulmillr/chokidar +[`ignore-by-default`]: https://github.com/novemberborn/ignore-by-default +[`--require` CLI 标志』: https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/readme.md#cli +[`--source` CLI 标志』: https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/readme.md#cli diff --git a/zh_CN/docs/recipes/when-to-use-plan.md b/zh_CN/docs/recipes/when-to-use-plan.md new file mode 100644 index 00000000..80ead7b0 --- /dev/null +++ b/zh_CN/docs/recipes/when-to-use-plan.md @@ -0,0 +1,139 @@ +___ +**备注** + +这是 [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/89767ec3b6174e59d37faaadb50cfa3c0d58bda6...master#diff-0c25d982e94d600cb6b8e438a0e67169) 用来查看本翻译与 AVA 的 master 分支是否有差别(如果你没有看到`when-to-use-plan.md`发生变化,那就意味着这份翻译文档是最新的)。 +___ + +# 什么时候使用`t.plan()` + +翻译:[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) + +AVA 和 [`tap`](https://github.com/tapjs/node-tap)/[`tape`](https://github.com/substack/tape) 的一个主要不同是`t.plan()`的行为。在 AVA 中`t.plan()`只是用来断言期望的断言数是否正确,并不会自动结束测试。 + +## `t.plan()`不好的用法 + +很多从`tap/tape`过渡过来的用户习惯在每个测试里大量使用`t.plan()`,尽管如此,在 AVA 里我们不认为这是一个“最佳实践”,我们反而认为`t.plan()`只有在少数情况才能提供其价值。 + +### 没有分支的同步测试 + +在大部分同步测试中`t.plan()`是没有必要的。 + +```js +test(t => { + // 不好:这里没有分支,t.plan 是无意义的 + t.plan(2); + + t.is(1 + 1, 2); + t.is(2 + 2, 4); +}); +``` + +`t.plan()`在这里并没有提供任何价值,并且如果你决定添加或删除断言时会带来额外的工作。 + +### Promises 期望得到 resolve + +```js +test(t => { + t.plan(1); + + return somePromise().then(result => { + t.is(result, 'foo'); + }); +}); +``` + +简单看一下,这个测试充分利用了`t.plan()`的优势,因为这里面涉及到了异步 promise 处理。尽管如此在这个测试里还有一些问题: + +1. `t.plan()`用在这里大概是为了防止`somePromise()`可能被 reject 的情况,但返回一个 reject 的 promise 测试始终会失败。 + +2. 使用`async`/`await`可能会更好。 + +```js +test(async t => { + t.is(await somePromise(), 'foo'); +}); +``` + +## `t.plan()`好的用法 + +`t.plan()`有很多可接受的用法。 + +### Promise 带上一个`.catch()`块 + +```js +test(t => { + t.plan(2); + + return shouldRejectWithFoo().catch(reason => { + t.is(reason.message, 'Hello') // 如果你关心的是 message 的话使用 t.throws() 更好 + t.is(reason.foo, 'bar'); + }); +}); +``` + +在这里,`t.plan()`用来确保`catch`块中的代码被执行,在大部分情况下,你更应该使用`t.throws()`断言,但这是一个可以接受的用法,因为`t.throws()`只允许你断言错误的`message`属性。 + +### 确保一个 catch 语句发生 + +```js +test(t => { + t.plan(2); + + try { + shouldThrow(); + } catch (err) { + t.is(err.message, 'Hello') // 如果你关心的是 message 的话使用 t.throws() 更好 + t.is(err.foo, 'bar'); + } +}); +``` + +像上面这种`try`/`catch`的情况,使用`t.throws()`一般是个更好的选择,但它只允许你断言错误的`message`属性。 + +### 确保多个 callback 被真正调用 + +```js +test.cb(t => { + t.plan(2); + + const callbackA = () => { + t.pass(); + t.end(); + }; + + const callbackB = () => t.pass(); + + bThenA(callbackA, callbackB); +}); +``` + +上面确保`callbackB`先被调用(且只调用一次),紧接着调用`callbackA`,其他的组合不会满足计划。 + +### 测试带有分支语句 + +在大部分情况下,在测试中使用复杂的分支是一个坏主意,一个明显的例外是用来测试自动生成的东西(可能来自一个 JSON 文档)。下面的`t.plan()`用来确保 JSON 输入的正确性: + +```js +const testData = require('./fixtures/test-definitions.json'); + +testData.forEach(testDefinition => { + test(t => { + const result = functionUnderTest(testDefinition.input); + + // testDefinition 应该有一个`foo`或`bar`的预期而不是同时满足它们 + t.plan(1); + + if (testDefinition.foo) { + t.is(result.foo, testDefinition.foo); + } + + if (testDefinition.bar) { + t.is(result.bar, testDefinition.foo); + } + }); +}); +``` + +## 总结 + +`t.plan()`有很多有效的使用方法,但不应该被盲目使用。一个好的经验法则是你的*测试*没有简单的,容易推断的,代码流的话你可以使用它。测试在 callback 里带有断言,`if`/`then`语句,`for`/`while`循环,并且(在一些情况下)`try`/`catch`块,都可以考虑使用`t.plan()`。 diff --git a/zh_CN/readme.md b/zh_CN/readme.md new file mode 100644 index 00000000..c07e535a --- /dev/null +++ b/zh_CN/readme.md @@ -0,0 +1,954 @@ +___ +**备注** + +这是 [readme.md](https://github.com/sindresorhus/ava/blob/master/readme.md) 的简体中文翻译。这个[链接](https://github.com/sindresorhus/ava/compare/67bf0472133041f59ef469f737b696de05ae316b...master#diff-0730bb7c2e8f9ea2438b52e419dd86c9) 用来查看本翻译与 AVA 的 master 分支是否有差别(如果你没有看到`readme.md`发生变化,那就意味着这份翻译文档是最新的)。 +___ + +#  + +> 未来的测试运行器 + +[](https://travis-ci.org/sindresorhus/ava) [](https://ci.appveyor.com/project/sindresorhus/ava/branch/master) [](https://coveralls.io/github/sindresorhus/ava?branch=master) [](https://gitter.im/sindresorhus/ava) + +虽然 JavaScript 是单线程,但在 Node.js 里由于其异步的特性使得 IO 可以并行。AVA 利用这个优点让你的测试可以并发执行,这对于 IO 繁重的测试特别有用。另外,测试文件可以在不同的进程里并行运行,让每一个测试文件可以获得更好的性能和独立的环境。在 Pageres 项目中从 Mocha[切换](https://github.com/sindresorhus/pageres/commit/663be15acb3dd2eb0f71b1956ef28c2cd3fdeed0) 到 AVA 让测试时间从 31 秒下降到 11 秒。测试并发执行强制你写原子测试,意味着测试不需要依赖全局状态或者其他测试的状态,这是一件非常好的事情。 + +*如果你想贡献(问题、PRs 等),请先阅读我们的[贡献向导](contributing.md)。* + +关注 [AVA 的 Twitter 账号](https://twitter.com/ava__js) 以获取最新信息。 + +翻译:[Español](https://github.com/sindresorhus/ava-docs/blob/master/es_ES/readme.md), [Français](https://github.com/sindresorhus/ava-docs/blob/master/fr_FR/readme.md), [日本語](https://github.com/sindresorhus/ava-docs/blob/master/ja_JP/readme.md), [Português](https://github.com/sindresorhus/ava-docs/blob/master/pt_BR/readme.md), [简体中文](https://github.com/sindresorhus/ava-docs/blob/master/zh_CN/readme.md) + +## 目录 + +- [用法](#用法) +- [CLI 用法](#cli) +- [配置](#配置) +- [文档](#文档) +- [API](#api) +- [断言](#断言) +- [小贴士](#小贴士) +- [FAQ](#faq) +- [秘方](#秘方) +- [支持](#支持) +- [相关](#相关) +- [链接](#链接) +- [团队](#团队) + + +## 为什么要用 AVA? + +- 轻量和高效 +- 简单的测试语法 +- 并发运行测试 +- 强制编写原子测试 +- 没有隐藏的全局变量 +- [为每个测试文件隔离环境](#隔离进程) +- [用 ES2015 编写测试](#支持es2015) +- [支持 Promise](#支持promise) +- [支持 Generator](#支持generator) +- [支持 Async](#支持async) +- [支持 Observable](#支持observable) +- [强化断言信息](#强化断言信息) +- [可选的 TAP 输出显示](#可选的tap输出) +- [简明的堆栈跟踪](#简明的堆栈跟踪) + + +## 测试语法 + +```js +import test from 'ava'; + +test(t => { + t.same([1, 2], [1, 2]); +}); +``` + + +## 用法 + +### 在项目中添加 AVA + +通过带`--inin`参数运行 AVA 全局安装命令,将会添加 AVA 到`package.json`: + +```console +$ npm install --global ava +$ ava --init +``` + +```json +{ + "name": "awesome-package", + "scripts": { + "test": "ava" + }, + "devDependencies": { + "ava": "^0.11.0" + } +} +``` + +写在`--init`后面的任何参数都会被添加到`package.json`。 + +#### 手动安装 + +你也可以直接安装 AVA: +```console +$ npm install --save-dev ava +``` + +你还需要配置`test`脚本在你的`package.json`,值为`ava`(参照上面的配置)。 + +### 创建你的测试文件 + +在你的工程根目录下创建一个名字为`test.js`的文件: + +```js +import test from 'ava'; + +test('foo', t => { + t.pass(); +}); + +test('bar', async t => { + const bar = Promise.resolve('bar'); + + t.is(await bar, 'bar'); +}); +``` + +