diff --git a/docs/Troubleshooting.md b/docs/Troubleshooting.md index 2114589f7..46c11018e 100644 --- a/docs/Troubleshooting.md +++ b/docs/Troubleshooting.md @@ -1,35 +1,35 @@ --- id: troubleshooting -title: Troubleshooting +title: 解决疑难 --- -Uh oh, something went wrong? Use this guide to resolve issues with Jest. +咦?遇到麻烦了吗?使用本节指南来解决Jest的相关问题! -## Tests are Failing and You Don't Know Why +## 测试失败了,但你却不知为何? -Try using the debugging support built into Node. Note: This will only work in Node.js 8+. +尝试使用Node内置的调试功能。注意:该功能仅支持Node.js 8以上版本 -Place a `debugger;` statement in any of your tests, and then, in your project's directory, run: +在你的任一个测试中声明一个`debugger;`,然后在你的项目目录中执行如下: ```bash node --inspect-brk node_modules/.bin/jest --runInBand [any other arguments here] -or on Windows +或者在Window系统: node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand [any other arguments here] ``` -This will run Jest in a Node process that an external debugger can connect to. Note that the process will pause until the debugger has connected to it. +这将让Jest运行在一个Node进程之中,该进程能被外部的调试器所连接。需要注意的是,这一进程直到调试器连接到它为止都处于暂停状态。 -To debug in Google Chrome (or any Chromium-based browser), open your browser and go to `chrome://inspect` and click on "Open Dedicated DevTools for Node", which will give you a list of available node instances you can connect to. Click on the address displayed in the terminal (usually something like `localhost:9229`) after running the above command, and you will be able to debug Jest using Chrome's DevTools. +若想要在谷歌浏览器(或者其他基于Chromium内核的浏览器)中调试,首先打开浏览器并转到`chrome://inspect`,然后点击“打开Node专用开发工具”,它将为你提供一个可以连接到的可用节点实例的列表。运行上述命令后,点击终端中所显示的地址(通常类似:`localhost:9229`),你将能够使用谷歌的开发者工具进行Jest调试。 -The Chrome Developer Tools will be displayed, and a breakpoint will be set at the first line of the Jest CLI script (this is done to give you time to open the developer tools and to prevent Jest from executing before you have time to do so). Click the button that looks like a "play" button in the upper right hand side of the screen to continue execution. When Jest executes the test that contains the `debugger` statement, execution will pause and you can examine the current scope and call stack. +接着谷歌开发者工具将展现出来,并在Jest CLI脚本的第一行设置一个断点(这样做是为了让你有时间打开开发者工具,并防止Jest在你准备好之前就执行)。然后,单击屏幕右上角类似“播放”按钮的地方继续执行。当Jest运行包含 `debugger` 语句的测试时,执行过程将暂停,此时你可以检查当前作用域并调用堆栈。 -> Note: the `--runInBand` cli option makes sure Jest runs test in the same process rather than spawning processes for individual tests. Normally Jest parallelizes test runs across processes but it is hard to debug many processes at the same time. +> 注意:`--runInBand` cli选项确保Jest在同一个进程中运行测试,而不是为单个测试生成进程。通常情况下,Jest 并行化测试会跨进程执行,但是很难同时调试多个进程。 -## Debugging in VS Code +## 在VS Code中调试 -There are multiple ways to debug Jest tests with [Visual Studio Code's](https://code.visualstudio.com) built in [debugger](https://code.visualstudio.com/docs/nodejs/nodejs-debugging). +使用[Visual Studio Code's](https://code.visualstudio.com)中内置的[调试器](https://code.visualstudio.com/docs/nodejs/nodejs-debugging)调试est测试的方法有多种。 -To attach the built-in debugger, run your tests as aforementioned: +要连接内置调试器,请按照如下步骤运行测试: ```bash node --inspect-brk node_modules/.bin/jest --runInBand [any other arguments here] @@ -37,7 +37,7 @@ or on Windows node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand [any other arguments here] ``` -Then attach VS Code's debugger using the following `launch.json` config: +然后使用以下`launch.json`配置来连接VS Code的调试器: ```json { @@ -53,7 +53,7 @@ Then attach VS Code's debugger using the following `launch.json` config: } ``` -To automatically launch and attach to a process running your tests, use the following configuration: +要自动启动并连接到运行测试的进程,请使用以下配置: ```json { @@ -76,7 +76,7 @@ To automatically launch and attach to a process running your tests, use the foll } ``` -or the following for Windows: +或者在Windows系统中使用: ```json { @@ -99,7 +99,7 @@ or the following for Windows: } ``` -If you are using Facebook's [`create-react-app`](https://github.com/facebookincubator/create-react-app), you can debug your Jest tests with the following configuration: +如果您使用的是Facebook的[`create-react-app`](https://github.com/facebookincubator/create-react-app),则可以使用以下配置调试Jest测试: ```json { @@ -120,51 +120,51 @@ If you are using Facebook's [`create-react-app`](https://github.com/facebookincu } ``` -More information on Node debugging can be found [here](https://nodejs.org/api/debugger.html). +更多在Node上调试的技巧可查阅[此处](https://nodejs.org/api/debugger.html)。 -## Debugging in WebStorm +## 在WebStorm中调试 -The easiest way to debug Jest tests in [WebStorm](https://www.jetbrains.com/webstorm/) is using `Jest run/debug configuration`. It will launch tests and automatically attach debugger. +在[WebStorm](https://www.jetbrains.com/webstorm/)中调试Jest测试的最简单方法就是使用`Jest run/debug configuration`命令。它将启动测试并自动连接调试器。 -In the WebStorm menu `Run` select `Edit Configurations...`. Then click `+` and select `Jest`. Optionally specify the Jest configuration file, additional options, and environment variables. Save the configuration, put the breakpoints in the code, then click the green debug icon to start debugging. +在WebStorm菜单 `Run`”中,选择`Edit Configurations...`,然后单击`+`并选择`Jest`,可选地指定Jest配置文件、附加选项和环境变量。保存配置,在代码中放置断点,然后单击绿色的调试图标开始调试。 -If you are using Facebook's [`create-react-app`](https://github.com/facebookincubator/create-react-app), in the Jest run/debug configuration specify the path to the `react-scripts` package in the Jest package field and add `--env=jsdom` to the Jest options field. +如果你使用的是Facebook的[`create react app`](https://github.com/facebookincubator/create-react-app),在Jest“运行/调试”配置的Jest package字段中指定`react-scripts`包的路径,并将`--env=jsdom` 添加到Jest options字段。 -## Caching Issues +## 缓存问题 -The transform script was changed or Babel was updated and the changes aren't being recognized by Jest? +转换脚本已更改或Babel已更新,但这些更改没有被Jest识别? -Retry with [`--no-cache`](CLI.md#--cache). Jest caches transformed module files to speed up test execution. If you are using your own custom transformer, consider adding a `getCacheKey` function to it: [getCacheKey in Relay](https://github.com/facebook/relay/blob/58cf36c73769690f0bbf90562707eadb062b029d/scripts/jest/preprocessor.js#L56-L61). +使用[`--no cache`](CLI.md#--cache)重试。Jest缓存转换后的模块文件以加快测试执行,如果您正在使用自己的自定义转换器,请考虑向其中添加一个`getCacheKey` 函数:[getCacheKey in Relay](https://github.com/facebook/relay/blob/58cf36c73769690f0bbf90562707eadb062b029d/scripts/jest/preprocessor.js#L56-L61)。 -## Unresolved Promises +## 未返回的Promises -If a promise doesn't resolve at all, this error might be thrown: +如果一个promise并未返回任何东西(no resolve),你将看到类似如下错误: ```bash - Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.` ``` -Most commonly this is being caused by conflicting Promise implementations. Consider replacing the global promise implementation with your own, for example `global.Promise = jest.requireActual('promise');` and/or consolidate the used Promise libraries to a single one. +通常情况下,这是由冲突的Promise实现所引起的。考虑使用你自己的promise实现替换全局的promise,例如:`global.Promise = jest.requireActual('promise');` 和/或 将使用过的promise库合并为单个库。 -If your test is long running, you may want to consider to increase the timeout by calling `jest.setTimeout` +如果测试长时间运行,你可能需要考虑通过调用`jest.setTimeout`来增加超时阈值。 ```js jest.setTimeout(10000); // 10 second timeout ``` -## Watchman Issues +## 守卫问题 -Try running Jest with [`--no-watchman`](CLI.md#--watchman) or set the `watchman` configuration option to `false`. +尝试在运行Jest时携带参数 [`--no-watchman`](CLI.md#--watchman) ,或者设置`watchman`配置选项为`false`。 -Also see [watchman troubleshooting](https://facebook.github.io/watchman/docs/troubleshooting). +更多详情,查看[守卫疑难排查](https://facebook.github.io/watchman/docs/troubleshooting)。 -## Tests are Extremely Slow on Docker and/or Continuous Integration (CI) server. +## 在Docker 和/或 持续集成(CI,Continuous Integration)服务器中运行est测试极慢 -While Jest is most of the time extremely fast on modern multi-core computers with fast SSDs, it may be slow on certain setups as our users [have](https://github.com/facebook/jest/issues/1395) [discovered](https://github.com/facebook/jest/issues/1524#issuecomment-260246008). +虽然Jest在现代多核电脑上运行的速度非常快,但在某些特殊设置上,Jest可能会很慢,就像我们的用户[已经](https://github.com/facebook/jest/issues/1395)[遇到的](https://github.com/facebook/jest/issues/1524#issuecomment-260246008)。 -Based on the [findings](https://github.com/facebook/jest/issues/1524#issuecomment-262366820), one way to mitigate this issue and improve the speed by up to 50% is to run tests sequentially. +根据[调查结果](https://github.com/facebook/jest/issues/1524\issuecomment-262366820)所示,一种减轻此问题并将速度提高50%的方法是按顺序运行测试。 -In order to do this you can run tests in the same thread using [`--runInBand`](CLI.md#--runinband): +为此,可以使用[`--runInBand`](CLI.md#--runinband)参数在同一线程中运行测试: ```bash # Using Jest CLI @@ -174,7 +174,7 @@ jest --runInBand yarn test --runInBand ``` -Another alternative to expediting test execution time on Continuous Integration Servers such as Travis-CI is to set the max worker pool to ~_4_. Specifically on Travis-CI, this can reduce test execution time in half. Note: The Travis CI _free_ plan available for open source projects only includes 2 CPU cores. +在持续集成服务器(如Travis-CI)上加快测试执行时间的另一种方法是将max worker pool设置为~_4_。特别是在Travis-CI上,这可以将测试执行时间减少一半。注意:可用于开源项目的Travis CI _free_ 计划仅包含2个CPU内核。 ```bash # Using Jest CLI @@ -184,15 +184,15 @@ jest --maxWorkers=4 yarn test --maxWorkers=4 ``` -## `coveragePathIgnorePatterns` seems to not have any effect. +## `coveragePathIgnorePatterns` 似乎没有起到作用 -Make sure you are not using the `babel-plugin-istanbul` plugin. Jest wraps Istanbul, and therefore also tells Istanbul what files to instrument with coverage collection. When using `babel-plugin-istanbul`, every file that is processed by Babel will have coverage collection code, hence it is not being ignored by `coveragePathIgnorePatterns`. +确保您没有额外使用 `babel-plugin-istanbul` 插件,Jest自身包装Istanbul,因此也规定了Istanbul要覆盖哪些文件。而使用`babel-plugin-istanbul`时,Babel处理的每个文件都将具有覆盖收集代码,因此`coveragePathIgnorePatterns`不会将其忽略。 -## Defining Tests +## 定义测试 -Tests must be defined synchronously for Jest to be able to collect your tests. +所有的测试必须被定义为同步性质,以便于Jest能够成功收集测试。 -As an example to show why this is the case, imagine we wrote a test like so: +举一个例子来说明为什么要这样规定,假设我们编写了这样一个测试: ```js // Don't do this it will not work @@ -201,10 +201,10 @@ setTimeout(() => { }, 0); ``` -When Jest runs your test to collect the `test`s it will not find any because we have set the definition to happen asynchronously on the next tick of the event loop. +当Jest运行测试来收集`test`时,它将找不到任何内容,因为我们已经将定义设置为在事件循环的下一个周期中异步执行。 -_Note:_ This means when you are using `test.each` you cannot set the table asynchronously within a `beforeEach` / `beforeAll`. +_注意_:这意味着当你使用 `test.each`方法时,你不能在`beforeEach` / `beforeAll`中异步设置表。 -## Still unresolved? +## 仍有疑问? -See [Help](/help.html). +请查阅[帮助文档](/help.html)。