# 测试 TypeScript

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

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

IntelliJ IDEA 为 [ts-node](https://typestrong.org/ts-node/) 和 [tsx](https://tsx.is/) 加载器提供开箱即用的配置，因此无需先将 TypeScript 测试编译为 JavaScript，即可运行和调试这些测试。 或者，可以安装自定义加载器，并按照 [创建 Node.js 测试运行程序运行/调试配置](#ts_test_custom_loader) 中的说明进行配置。

您可以直接从编辑器运行和调试测试，或启动   Node.js 测试运行器   类型的运行/调试配置。

## 开始之前

Procedure:

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

2. 安装 loader。 在命令行 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+Alt+1` (Visual Studio), `⌘ ⌃ 1` (Visual Studio (macOS)), `Alt+F12` (NetBeans), `Alt+F12` (Eclipse), `⌥ F12` (Eclipse (macOS))）中输入：

* `npm install --save-dev ts-node` 以安装 `ts-node`

* `npm install --save-dev tsx` 以安装 `tsx`

## 从编辑器运行测试

Procedure:

* 要运行测试，请点击其旁边装订区域中的 ![运行测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.run.svg)，然后从列表中选择   运行 '<test name>'  。

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

* 要运行套件中的所有测试，请点击其旁边装订区域中的 ![运行所有测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.actions.runAll.svg)，然后从列表中选择   运行 '<suite name>'  。

![使用 Node.js 内置测试运行器从编辑器运行套件](https://resources.jetbrains.com/help/img/idea/2026.3/ts_test_node_test_runner_run_suite_from_editor.animated.gif)

> **Tip:**
> 请注意，装订区域图标会根据其启动的测试和套件的状态而变化，详情请参见 [执行测试](performing-tests.html#gutter_icons_test_status)。

## 通过运行配置运行测试

通过运行/调试配置运行测试在您需要从多个测试文件运行测试时很有帮助。 使用运行/调试配置的另一个优势是可以配置自定义 loader。

![Node.js Test Runner 运行/调试配置](https://resources.jetbrains.com/help/img/idea/2026.3/调试配置](run-debug-configuration.html#temp_config)，其类型为   Node.js 测试运行器  。

Procedure:

1. 前往  `运行 | 编辑配置` 。 或者，在工具栏上的   运行   小部件中选择   编辑配置  。

![打开编辑配置对话框](https://resources.jetbrains.com/help/img/idea/2026.3/ws_client_ts_run_config_create.png)

或者，选择一个之前生成的临时 run/debug 配置。

2. 在打开的   编辑配置  对话框中，点击工具栏上的   添加  按钮（ !["添加"按钮](https://resources.jetbrains.com/help/img/idea/2026.3/调试配置：Node.js 测试运行器   对话框将打开。

3. 指定要使用的 Node.js 运行时、工作目录，以及要运行的测试范围。

4. 从   语言   列表中选择   TypeScript  。

5. 从   加载器   列表中选择要使用的加载程序。 默认情况下，开箱即用地集成了 [tsx](https://tsx.is/) 和 [ts-节点](https://typestrong.org/ts-node/) 。

或者，安装并配置自定义 loader。 例如，安装 [jiti](https://github.com/unjs/jiti)，并在   Node 选项   字段中键入  `-r jiti/register` 。

![TypeScript 测试：配置自定义加载器](https://resources.jetbrains.com/help/img/idea/2026.3/ts_test_node_js_test_runner_run_config_custom_loader.png)

6. 点击   运行  。

或者，从   运行  小部件中，选择一个现有的运行/调试配置并点击其旁边的 ![调试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.debug.svg)。

## 查看测试结果

测试结果显示在   运行   工具窗口中。

![测试结果](https://resources.jetbrains.com/help/img/idea/2026.3/ws_ts_test_results.png)

了解详情，请参阅 [探索测试结果](viewing-and-exploring-test-results.html)。

## 重新运行测试

Procedure:

* 要重新运行单个测试，请在   运行   工具窗口中选择它，然后从其上下文菜单中选择   运行 '<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` (Visual Studio), `Alt+Shift+R` (Visual Studio (macOS)), `Alt+Shift+R` (NetBeans), `Alt+Shift+G` (Eclipse), `⌘ ⌃ R` (Eclipse (macOS))。

![从运行工具窗口重新运行单个测试](https://resources.jetbrains.com/help/img/idea/2026.3/ts_testing_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) 可重新运行已忽略、失败或成功的测试，并从列表中选择   运行 '<test name>'  。

![从侧边栏重新运行单个测试](https://resources.jetbrains.com/help/img/idea/2026.3/ts_testing_node_test_runner_rerun_test_editor.animated.gif)

* 要重新运行整个套件，请点击套件旁边的装订区域图标，并从列表中选择   运行 '<suite name>'  。

![从侧边栏重新运行套件](https://resources.jetbrains.com/help/img/idea/2026.3/ts_testing_node_test_runner_rerun_suite_editor.animated.gif)

* 要重新运行上一次会话中的所有测试，请在   运行   工具窗口的工具栏上点击 ![重新运行“<套件名称>”图标](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.rerun.svg)。

![重新运行测试套件](https://resources.jetbrains.com/help/img/idea/2026.3/ts_testing_node_test_runner_rerun_suite_toolbar_icon.png)

## 调试测试

Procedure:

1. 请根据需要设置 [断点](using-breakpoints.html)。

2. 开始调试：

* 如需调试测试，请点击测试旁边边距中的 ![运行测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.run.svg)，然后从列表中选择 调试 '<test name>' 。

* 要调试套件中的所有测试，请点击套件旁边的 ![运行测试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.run.svg) ，然后从列表中选择 调试 '<suite name>' 。 ![从编辑器调试 TypeScript 测试套件](https://resources.jetbrains.com/help/img/idea/2026.3/ts_test_node_test_runner_debug_suite_from_editor.png)

* 或者，创建 Node.js 测试运行器 类型的运行/调试配置， [如上所述](#ts_test_node_native_test_runner_run_configuration_create)，从 运行 微件中选择该配置，然后点击其旁边的 ![调试图标](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.debug.svg)。 ![TypeScript 测试：通过 run/debug 配置调试测试](https://resources.jetbrains.com/help/img/idea/2026.3/ts_test_node_test_runner_debug_suite_from_rc.png)

