# 测试 React 应用程序

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

您可以在 [使用生成器创建的](react.html#creating_react_app_with_generators) React 应用程序中运行和调试 [Jest 测试](https://facebook.github.io/jest/)。 在开始之前，请确保  react-scripts  包已添加到您的  dependencies  对象中  `package.json` 。

您可以直接从编辑器、   项目   工具窗口或者通过运行/调试配置来运行和调试 Jest 测试。 如需了解更多信息，请参阅 [Jest](running-unit-tests-on-jest.html)。

## 从编辑器运行测试

Procedure:

* 点击装订区域中的 ![运行图标](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>  。

![从编辑器运行单个测试](https://resources.jetbrains.com/help/img/idea/2026.3/ws_run_test_from_editor_600.animated.gif) 您还可以在编辑器中直接查看测试是否通过或失败，这要归功于  测试状态  图标 ![测试通过](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)。

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

### 创建 Jest 运行/调试配置

Procedure:

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

> **Tip:**
> 或者，在   项目  工具窗口（ `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)), `⌘ 1` (Xcode), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (macOS)), `Alt+1` (ReSharper), `⌘ 1` (ReSharper (macOS)), `Alt+X` (QtCreator), `⌃ X` (QtCreator (macOS)), `Ctrl+1` (NetBeans), `Alt+1` (Eclipse), `Alt+1` (Eclipse (macOS))）中选择一个测试文件，然后从上下文菜单中选择   创建 <文件名>  。

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

3. 在   Jest 包  字段中，指定  react-scripts 包的路径。

4. 在   Jest 选项  字段中，输入  `--env=jsdom` 。

![测试 React：Jest 运行配置](https://resources.jetbrains.com/help/img/idea/2026.3/react_run_config_jest.png)

### 通过运行/调试配置启动测试

Procedure:

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

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

## 调试测试

Procedure: 调试测试

1. 从主工具栏的列表中选择 Jest 运行/调试配置，并点击右侧的 ![调试按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.debug.svg) 。

2. 在打开的 [调试工具窗口](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)。

