# 测试 Node.js

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/go/llms.txt).

> **TL;DR**
> 必需插件  :
>
>
>
> `JavaScript 和 TypeScript` ：该插件与 GoLand 捆绑在一起，并默认启用。
>
>
>
> `Node.js` （仅用于开发 Node.js 应用程序）：在   设置 | 插件   页面上安装该插件。

使用 GoLand，您可以使用许多框架测试 Node.js 应用程序。

当前页面描述了使用 [内置 Node.js 测试运行器](https://nodejs.org/en/blog/announcements/v20-release-announce#stable-test-runner) 或 [Mocha](http://mochajs.org/) 进行测试，Mocha 是一个 JavaScript 测试框架，特别适合执行异步测试场景。 您可以从 GoLand 外部运行 Mocha 测试，检查按树视图排列的测试结果，并轻松导航到测试源代码。 编辑器中测试旁边，GoLand 显示测试状态，并提供快速运行或调试的选项。

> **Note:**
> 自 2018 年 5 月起，Nodeunit 已被弃用。 如需更多信息，请参考 [Nodeunit official website](https://github.com/caolan/nodeunit#nodeunit)。

## 在开始之前

Procedure:

1. 下载并安装 [Node.js](http://nodejs.org/#download)。

2. 请确保在设置中启用了  JavaScript 和 TypeScript  插件。 按下 `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Comma` (VSCode), `⌘ Comma` (VSCode (macOS)) 打开设置，然后选择  `插件` 。 在搜索字段中，键入  JavaScript 和 TypeScript 。 有关插件的更多信息，请参阅 [管理插件](managing-plugins.html)。

3. 按照 [从 JetBrains Marketplace 安装插件](managing-plugins.html#install_plugin_from_repo) 中的说明，在   设置 | 插件   页面上安装并启用  Node.js  插件。

## 内置 Node.js 测试运行器

从第 20 版开始，Node.js 自带一个稳定版本的 [内置 Node.js 测试运行器](https://nodejs.org/en/blog/announcements/v20-release-announce#stable-test-runner)。 GoLand 支持与内置测试运行器集成，因此您可以在不安装和配置任何第三方框架的情况下运行您的测试。

测试 TypeScript 代码也是支持的，详情请参见 [测试 TypeScript](testing-typescript.html)。

Procedure:

1. 请确保您的计算机已安装 20 或更高版本的 Node.js，并将其配置为 [本地 Node.js 运行时](developing-node-js-applications.html#ws_node_configure_local_node_interpreter)。

2. 如 [Node.js 官方网站](https://nodejs.org/dist/latest-v18.x/docs/api/test.html) 所述，创建测试文件。

3. 在滚动条区：

* 点击 ![运行测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.gutter.run.svg) 旁边的单个测试以运行它。

* 点击 ![运行所有测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.actions.runAll.svg) 旁边的测试套件以运行其中的所有测试。

![从编辑器运行测试，使用 Node.js 内置测试运行器](https://resources.jetbrains.com/help/img/idea/2026.3/node_test_runner_gutter_icons.png)

4. 请查看   运行   工具窗口中的测试结果，详细了解请参阅 [深入探讨测试结果](viewing-and-exploring-test-results.html)。

5. 重新运行测试：

* 要重新运行单个测试，请在 运行 工具窗口中选择它，然后从其上下文菜单中选择 运行“<test name>” ，或按 `Alt+Shift+R` (Windows), `⌘ ⌃ R` (macOS), `⌘ ⌃ R` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ R` (macOS System Shortcuts), `Alt+Shift+R` (XWin), `Alt+Shift+R` (GNOME), `Alt+Shift+R` (KDE), `Alt+Shift+R` (Emacs), `Alt+Shift+R` (Sublime Text), `⌘ ⌃ R` (Sublime Text (macOS)), `Alt+Shift+R` (VSCode), `Alt+Shift+R` (VSCode (macOS))。 ![从运行工具窗口重新运行单个测试](https://resources.jetbrains.com/help/img/idea/2026.3/node_test_runner_rerun_test_from_run_tool_window.png) 或者，使用 测试旁边的 间距 图标重新运行。 点击 ![运行忽略测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.gutter.runFailed.svg)、 ![重新运行失败的测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.gutter.runError.svg) 或 ![重新运行成功的测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.gutter.runSuccess.svg) 来重新运行被忽略的、失败的或成功的测试。 ![从侧边栏重新运行单个测试](https://resources.jetbrains.com/help/img/idea/2026.3/node_test_runner_rerun_test_gutter.png)

* 要重新运行整个套件，请点击 ![重新运行“<suite name>”图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.rerun.svg)工具窗口的 运行 工具栏。 ![重新运行测试套件](https://resources.jetbrains.com/help/img/idea/2026.3/node_test_runner_rerun_suite_toolbar_icon.png) 或者，点击套件旁边的槽图标。 ![从侧边栏重新运行套件](https://resources.jetbrains.com/help/img/idea/2026.3/node_test_runner_rerun_suite_gutter.png)

## 使用第三方测试框架测试您的代码

虽然您可以使用任何框架，但推荐的框架是 Mocha。

Procedure: 安装 Mocha

1. 在命令行 shell 或嵌入式   终端  （ `Alt+F12` (Windows), `⌥ F12` (macOS), `⌥ F12` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ T` (macOS System Shortcuts), `Alt+F12` (XWin), `Alt+F12` (GNOME), `Alt+F12` (KDE), `Alt+F12` (Emacs), `Alt+F12` (Sublime Text), `⌥ F12` (Sublime Text (macOS)), `Ctrl+`` (VSCode), `⌃ `` (VSCode (macOS))）中，输入以下命令之一：

* `npm install mocha` 以在您的项目中进行本地安装。

* `npm install -g mocha` 以全局安装。

* `npm install --save-dev mocha` 将 Mocha 作为 [开发依赖项](https://docs.npmjs.com/getting-started/using-a-package.json#specifying-packages)安装。

可通过 [Mocha 官方网站上的入门指南](https://mochajs.org/#installation)了解更多信息。

2. 请安装 [Chai 期望库](https://www.chaijs.com/) 作为 [Node.js 标准 assert 函数](https://nodejs.org/api/assert.html)的高效替代品。

在命令行 shell 或嵌入式   终端  （ `Alt+F12` (Windows), `⌥ F12` (macOS), `⌥ F12` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ T` (macOS System Shortcuts), `Alt+F12` (XWin), `Alt+F12` (GNOME), `Alt+F12` (KDE), `Alt+F12` (Emacs), `Alt+F12` (Sublime Text), `⌥ F12` (Sublime Text (macOS)), `Ctrl+`` (VSCode), `⌃ `` (VSCode (macOS))）中，输入：

`npm install --save-dev chai`

### 编写 Mocha 测试

Procedure:

* 根据 [Mocha official website](https://mochajs.org/) 的指示创建测试。

### 运行 Mocha 测试

使用 GoLand，您可以直接从编辑器快速运行单个 Mocha 测试，或者创建运行/调试配置来执行某些或全部测试。

Procedure: 从编辑器运行单个 Mocha 测试

* 点击装订区域中的 ![“运行”按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.run.svg) 或 ![重新运行按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.rerun.svg)，并从列表中选择   运行 <test_name>  。

![从编辑器运行 Mocha 测试](https://resources.jetbrains.com/help/img/idea/2026.3/ws_node_test_run_single_test.png)

您还可以在编辑器中查看测试是否通过或失败，由于装订区域的  测试状态 图标 ![测试通过](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.green2.svg) 和 ![测试失败](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.red2.svg)。

![侧栏中的测试状态](https://resources.jetbrains.com/help/img/idea/2026.3/ws_node_test_mocha_status_gutter.png)

Procedure: 创建一个 Mocha 运行配置

1. 打开 [运行/调试配置](null)对话框（  `运行 | 编辑配置` 在主菜单中），点击 ![添加按钮](https://resources.jetbrains.com/help/img/idea/2026.3/调试配置：Mocha](run-debug-configuration-mocha.html) 对话框打开。

2. 指定要使用的 Node.js 运行时以及  `mocha`  包的位置。

3. 指定应用程序的工作目录。 默认情况下，   工作目录   字段显示项目根文件夹。 若要更改此预定义设置，请指定所需文件夹的路径。

4. 可选：

在相关源文件发生更改时自动配置重新运行测试。 要做到这一点，请在  `--watch`  标志中添加   其他 Mocha 选项   字段。

5. 指定要运行的测试。 这可以是一个特定的测试或套件，整个测试文件或包含测试文件的文件夹。

您还可以定义模式以仅运行来自匹配文件的测试，例如，  `*.test.js` 。 如果测试文件存储在单独的文件夹中，例如，  `test` ，请在相对于工作目录的模式中指定此文件夹的路径：

```SHELL
./folder1/folder2/test/*.test.js

```

6. 选择用于运行测试的 [接口](http://mochajs.org/#interfaces)。

Procedure: 通过 Mocha 运行配置运行测试

1. 从配置列表中选择 Mocha 运行/调试配置，然后在列表或工具栏中点击 ![运行图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.run.svg)。

2. 在   测试运行器   选项卡的   运行   工具窗口中监控测试执行并分析测试结果。 如需更多信息，请参阅 [探索测试结果](viewing-and-exploring-test-results.html)。

* 请使用 点击查看差异 链接打开 差异查看器 并将实际结果与预期进行比较。

* 所选测试文件的名称显示为链接。 点击此链接跳转到源代码。

3. 在   测试运行器   选项卡的   运行   工具窗口中监控测试执行并分析测试结果。 如需更多信息，请参阅 [探索测试结果](viewing-and-exploring-test-results.html)。

![监控测试执行](https://resources.jetbrains.com/help/img/idea/2026.3/ws_node_test_mocha_monitor_tests_and_navigation.png)

![Mocha：监控测试](https://resources.jetbrains.com/help/img/idea/2026.3/ws_node_test_mocha_monitor_tests_and_navigation.png)

Procedure: 重新运行失败的 Mocha 测试

* 点击 ![重新运行失败测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.restartFailedTests.svg)测试结果工具栏。 GoLand 将执行上一会话中失败的所有测试。

![Mocha：重新运行所有失败的测试](https://resources.jetbrains.com/help/img/idea/2026.3/ws_node_test_mocha_rerun_failed_tests.png)

* 若要重新运行某个特定失败的测试，请在其上下文菜单中选择  `运行 <test name>` 。

![在编辑器中重新运行单个失败的测试：上下文菜单](https://resources.jetbrains.com/help/img/idea/2026.3/ws_node_test_mocha_rerun_one_failed_test_context_menu.png)

* 或者，点击 ![失败的测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.red2.svg) 在失败的测试旁边的装订区域，然后从列表中选择  `运行 <test name>` 。

![从编辑器重新运行单个失败的测试：间距图标](https://resources.jetbrains.com/help/img/idea/2026.3/ws_node_test_mocha_rerun_one_failed_test_gutter_icon.png)

有关更多信息，请参阅 [重新运行测试](performing-tests.html#rerun)。

### 导航

使用 GoLand ，您可以在文件与相关测试文件之间跳转，或从 [测试运行器选项卡](test-runner-tab.html)中的测试结果跳转到测试。

> **Tip:**
> 这样的导航仅在测试文件遵循最受欢迎的的命名约定时才有效，例如，具有  `.测试.` 、  `.spec.`  或  `_spec.`  后缀，并且位于源文件旁边或  `test`  文件夹中。

Procedure:

* 在编辑器中打开文件，选择上下文菜单中的  `转到 | 测试`  或  `转到 | 测试对象` ，或者直接按 `Ctrl+Shift+T` (Windows), `⌘ ⇧ T` (macOS), `⌘ ⇧ T` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ T` (macOS System Shortcuts), `Ctrl+Shift+T` (XWin), `Ctrl+Shift+T` (GNOME), `Ctrl+Shift+T` (KDE), `Ctrl+Shift+T` (Emacs), `Ctrl+Shift+T` (Sublime Text), `Ctrl+Shift+T` (Sublime Text (macOS)), `Ctrl+Shift+T` (VSCode), `Ctrl+Shift+T` (VSCode (macOS))，即可在测试和其主题之间进行切换。

* 从测试结果跳转到测试定义，双击   测试运行器   选项卡中的测试名称，或从上下文菜单中选择  `跳转到源` ，或者按 `F4` (Windows), `⌘ ↓` (macOS), `F4` (IntelliJ IDEA Classic (macOS)), `F4` (macOS System Shortcuts), `F4` (XWin), `F4` (GNOME), `F4` (KDE), `F4` (Emacs), `F4` (Sublime Text), `F4` (Sublime Text (macOS)), `F4` (VSCode), `F4` (VSCode (macOS))。 测试文件在编辑器中打开时，文本光标放置在测试定义处。

* 对于失败的测试，GoLand 会从堆栈跟踪中将您带到测试中的失败行。 如果精确的行不在栈跟踪中， 您将被带到测试定义。

### 调试测试

如果测试由于不明确的原因失败，您可以调试该测试以确定测试是否已过时，或者是您对源代码所做的更改破坏了应用程序的预期行为。

使用 GoLand，您可以直接从编辑器快速开始调试单个 Mocha 测试，或者创建一个运行/调试配置来调试部分或全部测试。

Procedure: 从编辑器开始调试单个测试

1. 请在要调试的测试旁边的装订区域设置断点。 您可以通过双击   运行   工具窗口中的失败测试或按 `F4` (Windows), `⌘ ↓` (macOS), `F4` (IntelliJ IDEA Classic (macOS)), `F4` (macOS System Shortcuts), `F4` (XWin), `F4` (GNOME), `F4` (KDE), `F4` (Emacs), `F4` (Sublime Text), `F4` (Sublime Text (macOS)), `F4` (VSCode), `F4` (VSCode (macOS)) 跳转到失败的测试。

2. 点击装订区域中的 ![“运行”按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.run.svg) 或 ![重新运行按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.rerun.svg)，并从列表中选择   调试 <test_name>  。

3. 在打开的   调试   工具窗口中，按常规操作： [逐步执行程序](stepping-through-the-program.html)、 [停止并恢复](null) 程序执行、 [在暂停时检查程序](examining-suspended-program.html)，探索调用堆栈和变量，设置监视，评估变量， [查看实际的 HTML DOM](viewing-actual-html-dom.html) 等。

![调试 Mocha 测试](https://resources.jetbrains.com/help/img/idea/2026.3/go_node_test_mocha_debug.png)

Procedure: 使用 Mocha 运行/调试配置进行调试测试

1. 将断点设置在您要调试的测试旁边。 您可以通过双击   运行   工具窗口中的失败测试或按 `F4` (Windows), `⌘ ↓` (macOS), `F4` (IntelliJ IDEA Classic (macOS)), `F4` (macOS System Shortcuts), `F4` (XWin), `F4` (GNOME), `F4` (KDE), `F4` (Emacs), `F4` (Sublime Text), `F4` (Sublime Text (macOS)), `F4` (VSCode), `F4` (VSCode (macOS)) 跳转到失败的测试。

2. 创建一个 Mocha 运行/调试配置 [如上所述](#createRunConfigMocha)。

3. 从配置列表中选择 Mocha 运行/调试配置，然后在列表或工具栏中点击 ![调试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.run.svg)。

4. 在打开的 [调试工具窗口](debug-tool-window.html) 中，按常规操作： [逐步执行程序](stepping-through-the-program.html)， [停止并恢复](null) 程序执行， [在挂起时检查程序](examining-suspended-program.html)，探索调用堆栈和变量，设置监视，评估变量， [查看实际 HTML DOM](viewing-actual-html-dom.html) 等。

要忽略源代码映射并逐步执行实际运行的代码，请在   调试   工具窗口的工具栏上按 ![禁用源代码映射切换按钮](https://resources.jetbrains.com/help/img/idea/2026.3/sourceMapDisable.png)。

了解更多详细信息，请参阅 [Debug failed tests](performing-tests.html#RedebugFailedTests)。

### 监控代码覆盖率

使用 GoLand，您还可以监控代码中有多少部分 [通过 Mocha 测试覆盖](http://en.wikipedia.org/wiki/Code_coverage)。 GoLand 会在专用的   覆盖率  工具窗口中显示此统计信息，并在编辑器和   项目  工具窗口（ `Alt+1` (Windows), `⌘ 1` (macOS), `⌘ 1` (IntelliJ IDEA Classic (macOS)), `⌘ 1` (macOS System Shortcuts), `Alt+1` (XWin), `Alt+1` (GNOME), `Alt+1` (KDE), `Alt+1` (Emacs), `Alt+1` (Sublime Text), `⌘ 1` (Sublime Text (macOS)), `Ctrl+Shift+E` (VSCode), `⌘ ⇧ E` (VSCode (macOS))）中以可视化方式标记已覆盖和未覆盖的行。 要监控覆盖率，您需要安装 [nyc](https://github.com/istanbuljs/nyc)，这是 [Istanbul](https://istanbul.js.org/)的命令行界面。

Procedure: 安装 nyc

* 在命令行 shell 或嵌入式   终端  （ `Alt+F12` (Windows), `⌥ F12` (macOS), `⌥ F12` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ T` (macOS System Shortcuts), `Alt+F12` (XWin), `Alt+F12` (GNOME), `Alt+F12` (KDE), `Alt+F12` (Emacs), `Alt+F12` (Sublime Text), `⌥ F12` (Sublime Text (macOS)), `Ctrl+`` (VSCode), `⌃ `` (VSCode (macOS))）中，输入：

`npm install --save-dev nyc`

> **Tip:**
> 您还可以安装 Istanbul 本身，1.1.0 版或更高版本。 这些版本支持 ES6 和 TypeScript。

Procedure: 运行测试并生成覆盖率报告

1. 启动测试：

* 创建 Mocha 运行/调试配置， [如上所述](#createRunConfigMocha)，在主工具栏中从列表中选择它，然后点击该列表右侧的 ![运行覆盖率](https://resources.jetbrains.com/help/img/idea/2026.3/app.general.runWithCoverage.svg)。

* 或者，从编辑器运行特定的套件或测试覆盖率：点击左侧凹槽中的 ![“运行”按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.run.svg) 或 ![重新运行按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.rerun.svg) 并从列表中选择 运行 <test_name> 并生成覆盖率 。

2. 在 [Coverage](coverage-tool-window.html) 工具窗口中监控代码覆盖率。 报告显示了多少文件经过了测试及其中已覆盖代码行的百分比。 从报告中可以跳转到文件，查看哪些行已覆盖–标记为绿色–哪些行未覆盖–标记为红色。

![Mocha 测试：覆盖率报告](https://resources.jetbrains.com/help/img/idea/2026.3/go_mocha-coverage-report.png)

### 在 Docker 容器内使用 Node.js 运行测试

通过 GoLand，您可以在 Docker 容器中运行 [Mocha](http://mochajs.org/) 测试，其方式与本地运行相同。

Procedure: 在开始之前

1. 安装  Node.js  和  Node.js 远程解释器  插件（在   设置 | 插件   页面上），如 [从 JetBrains Marketplace 安装插件](managing-plugins.html#install_plugin_from_repo) 中所述。 。.

2.

3. 下载、安装并配置  Docker ，如 [Docker](docker.html)中所述.

4. [在 Docker 中配置 Node.js 远程解释器](node-with-docker.html#ws_node_docker_configure_interpreter)，或通过 [Docker Compose](node-with-docker-compose.html#ws_node_docker_compose_configure_interpreter)，并在您的项目中 [将其设置为默认](node-with-docker.html#ws_node_docker_manage_dependencies)。 此外，请确保与此远程解释器关联的包管理器已 [设置为项目默认](node-with-docker.html#ws_node_docker_manage_dependencies)。

5. 打开您的  `package.json`  并确保所需的测试框架列在  `devDependencies`  部分：

```JSON
{
    "name": "node-express",
    "version": "0.0.0",
    "private": true,
    "dependencies": {
        "cookie-parser": "~1.4.4",
        "debug": "~2.6.9",
        "express": "~4.16.1",
        "http-errors": "~1.6.3",
        "morgan": "~1.9.1",
        "pug": "^3.0.2"
    },
    "devDependencies": {
        "chai": "^4.3.4",
        "concurrently": "^6.3.0",
        "eslint": "^8.1.0",
        "http-server": "^14.0.0",
        "jest": "^27.3.1",
        "mocha": "^9.1.3",
        "nyc": "^15.1.0"
    }
}

```

6. 在编辑器中的任意位置右键点击，然后从上下文菜单中选择   运行 '<package manager> install'  。

Procedure: 运行测试

1. 根据 [Mocha official website](https://mochajs.org/) 的指示创建测试。

2. 如同本地开发：直接从编辑器运行和调试单个测试，或者按照上面 [运行测试](#ws_node_test_overview) 和 [调试测试](#ws_node_test_mocha_debug) 中的描述创建一个运行/调试配置来启动部分或全部测试。

