From cf6292db82bce4177ff5ee161457aa47ef95625a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A2=E6=B5=A9=E9=B9=8F?= Date: Thu, 6 Dec 2018 13:17:11 +0800 Subject: [PATCH] Translation for zh-CN --- .../zh/buildTools/Testing-with-NPM.html | 129 ++++++++---------- 1 file changed, 60 insertions(+), 69 deletions(-) diff --git a/docs/manual/zh/buildTools/Testing-with-NPM.html b/docs/manual/zh/buildTools/Testing-with-NPM.html index 3f6a7c4aef..7eb6cb0174 100644 --- a/docs/manual/zh/buildTools/Testing-with-NPM.html +++ b/docs/manual/zh/buildTools/Testing-with-NPM.html @@ -8,69 +8,67 @@ -

[name]

+

使用NPM进行测试([name])

- This article shows how to get three.js into a [link:https://nodejs.org/en/ node.js] environment so that you - can execute automated tests. Tests can be run on the command line, or by automated - CI tools like [link:https://travis-ci.org/ Travis]. + 这篇文章展示了如何将three.js置入[link:https://nodejs.org/en/ node.js]环境中, + 这样你就可以执行自动化测试了。测试可以通过命令行或者类似[link:https://travis-ci.org/ Travis]的CI工具来运行。

-

The short version

+

一句话概括

- If you're comfortable with node and npm, + 如果你习惯使用node和npm, $ npm install three --save-dev - and add + 并将 var THREE = require('three'); - to your test. + 添加到你的测试中。

-

Create a testable project from scratch

+

从头创建一个可测试的项目

- If you're not familiar with these tools, here's a quick guide (for linux, the installation process - will be slightly different using windows, but the NPM commands are identical). + 如果你不太熟悉这些工具,下面是一个快速入门。(基于linux,在windows上的安装过程会稍稍有点不一样,不过NPM指令是相同的。)

-

Basic setup

+

基本设置

  1. - Install [link:https://www.npmjs.org/ npm] and nodejs. The shortest path typically looks something like + 安装[link:https://www.npmjs.org/ npm]和nodejs。最简单的方式一般像这样 $ sudo apt-get install -y npm nodejs-legacy -# fix any problems with SSL in the default registry URL +# 修复默认registry URL中任何SSL的问题 $ npm config set registry http://registry.npmjs.org/
  2. - Make a new project directory + 新建一个项目路径 $ mkdir test-example; cd test-example
  3. - Ask npm to create a new project file for you: + 让npm为你创建一份新的项目文件: $ npm init - and accept all defaults by hitting Enter on all the prompts. - This will create package.json. + 在所有出现的提示中敲击回车键来接受默认值。 + 这样,一份package.json就建立好了。

  4. - Try and start the test feature with + 尝试启动测试功能 $ npm test - This will fail, which is expected. - If you look in the package.json, the definition of the test script is + 当然,这一定会失败。 + 如果你检查一下package.json,test script的定义是这样的 "test": "echo \"Error: no test specified\" && exit 1" @@ -79,78 +77,75 @@ $ npm test
-

Add mocha

+

添加mocha

- We're going to use [link:https://mochajs.org/ mocha]. + 我们将使用[link:https://mochajs.org/ mocha]。
  1. - Install mocha with + 安装mocha $ npm install mocha --save-dev - Notice that node_modules/ is created and your dependencies appear in there. - Also notice that your package.json has been updated: the property devDependencies - is added and updated by the use of --save-dev. + 你会注意到 node_modules/ 被创建了,并且你的依赖都出现在了这里面。 + 还有你的package.json被更新了,--save-dev指令向其中加入并更新了devDependencies属性。

  2. - Edit package.json to use mocha for testing. When test is invoked, we just want to run - mocha and specify a verbose reporter. By default this will run anything in test/ - (not having directory test/ can run into npm ERR!, create it by mkdir test) + 编辑package.json来使用mocha进行测试。当调用测试的时候,我们只想运行mocha并且生成一份详细的报告。 + 默认情况下这会运行 test/ 中的任何东西。 + (如果项目中没有 test/ 目录的话,会导致npm报错。你可以通过mkdir test来创建这个目录) "test": "mocha --reporter list"
  3. - Rerun the test with + 重新运行测试 $ npm test - This should now succeed, reporting 0 passing (1ms) - or similar. + 现在应该就能成功执行了,生成类似 0 passing (1ms) 的报告。
-

Add three.js

+

添加three.js

  1. - Let's pull in our three.js dependency with + 现在添加我们的three.js依赖 $ npm install three --save-dev
    • - If you need a different three version, use + 如果你需要three.js的其他版本,使用 $ npm show three versions - to see - what's available. To tell npm the right one, use + 来确认哪些是可用的。要让npm使用正确的版本,执行 $ npm install three@0.84.0 --save - (0.84.0 in this example). --save makes this a dependency of this project, rather than - dev dependency. See the docs [link:https://www.npmjs.org/doc/json.html here] for more info. + (例子中用的是0.84.0)。 --save 指令将此加入项目的dependency而不是dev dependency。 + 更多信息请参阅这份文档
  2. - Mocha will look for tests in test/, so let's + Mocha会在 test/ 目录中寻找测试文件,所以我们先创建这个目录: $ mkdir test
  3. - Finally we actually need a JS test to run. Let's add a simple test that will verify that - the three.js object is available and working. Create test/verify-three.js containing: + 最后我们需要一份JS测试文件来运行。我们就添加一段简单的测试程序,这段程序会检验three.js对象是否能正常工作。 + 在 test/ 目录下创建verify-three.js包含以下代码: var THREE = require('three'); var assert = require("assert"); @@ -169,8 +164,7 @@ describe('The THREE object', function() {
  4. - Finally let's test again with $ npm test. This should run the tests above and succeed, - showing something like: + 最后再次通过$ npm test来测试。这次应该能正确执行上面的代码,并且返回类似: The THREE object should have a defined BasicShadowMap constant: 0ms The THREE object should be able to construct a Vector3 with default of x=0: 0ms @@ -180,33 +174,33 @@ The THREE object should be able to construct a Vector3 with default of x=0: 0ms
-

Add your own code

+

加入你自己的代码

- You need to do three things: + 你需要做下面三件事:
  1. - Write a test for the expected behaviour of your code, and place it under test/. - [link:https://github.com/air/encounter/blob/master/test/Physics-test.js Here] is an example from a real project. + 为你的代码写一段测试程序来检验期望结果,并把它放在 test/ 目录下。 + 这里有一个实际项目的例子。
  2. - Export your functional code in such a way that nodejs can see it, for use in conjunction with require. - See it [link:https://github.com/air/encounter/blob/master/js/Physics.js here]. + 将你的代码以nodejs承认的方式导出,即可以通过require的方式引用。 + 参考这份代码
  3. - Require your code into the test file, in the same way we did a require('three') in the example above. + 在测试程序中通过require引入你自己的代码,就像上面例子中我们通过require('three')来引入一样。

- Items 2 and 3 will vary depending on how you manage your code. In the example of Physics.js - given above, the export part is right at the end. We assign an object to module.exports: + 第2、3条会根据你组织代码的方式而改变。在上面给出的Physics.js的例子中,导出的部分在代码的最末尾。 + 我们将module.exports赋值为一个对象:

//============================================================================= -// make available in nodejs +// 为了在nodejs中可用 //============================================================================= if (typeof exports !== 'undefined') { @@ -215,38 +209,35 @@ if (typeof exports !== 'undefined')
-

Dealing with dependencies

+

处理依赖

- If you're already using something clever like require.js or browserify, skip this part. + 如果你已经在使用require.js或者browserify之类的便捷工具,就跳过这个部分。

- Typically a three.js project is going to run in the browser. Module loading is hence done by - the browser executing a bunch of script tags. Your individual files don't have to worry - about dependencies. In a nodejs context however, there is no index.html binding everything - together, so you have to be explicit. + 一般来说,一个three.js项目将在浏览器中运行,浏览器会通过执行一系列script标签来加载模块。 + 你自己的文件不用考虑依赖的问题。然而在nodejs环境中,没有一个关联所有文件的index.html,所以你需要显式地加载。

- If you're exporting a module that depends on other files, you're going to have to tell node to load them. - Here is one approach: + 如果你要导出的模块还依赖其他文件,你需要告诉node去加载它们。下面是一种方式:

  1. - At the start of your module, check to see if you're in a nodejs environment. + 在你的模块顶部,检查是否处于nodejs环境中。
  2. - If so, explicitly declare your dependencies. + 如果是,那就显式地声明你的依赖。
  3. - If not, you're probably in a browser so you don't need to do anything else. + 如果不是,你多半处于浏览器环境中。这时候你不需要做任何多余操作。
- Example code from Physics.js: + 用Physics.js中的代码举例: //============================================================================= -// setup for server-side testing +// 服务器端测试配置 //============================================================================= -if (typeof require === 'function') // test for nodejs environment +if (typeof require === 'function') // 检测nodejs环境 { var THREE = require('three'); var MY3 = require('./MY3.js'); -- GitLab