# 测试 React 应用程序

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com.cn/help/idea/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.cn/help/img/idea/2026.3/app-client.expui.run.run.svg) 或 ![重新运行图标](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.actions.rerun.svg)，并从列表中选择   运行 <test_name>  。

![从编辑器运行单个测试](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_run_test_from_editor_600.animated.gif) 还可以直接在编辑器中通过装订区域中的  测试状态 图标 ![测试通过](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.runConfigurations.testState.green2.svg) 和 ![测试失败](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.runConfigurations.testState.red2.svg) 查看测试是否通过或失败。

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

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

Procedure:

1. 打开 [运行/调试配置](run-debug-configurations-dialog.html)对话框（  `运行 | 编辑配置` 在主菜单中），点击 !["添加"按钮](https://resources.jetbrains.com.cn/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)), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (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.cn/help/img/idea/2026.3/react_run_config_jest.png)

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

Procedure:

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

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

## 调试测试

Procedure: 调试测试

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

2. 在打开的 [调试工具窗口](debug-tool-window.html) 中，按常规方式进行操作： [单步执行程序](stepping-through-the-program.html)、 [停止并恢复](starting-the-debugger-session.html#pause-resume)程序执行、 [在暂停时检查](examining-suspended-program.html)、查看调用堆栈和变量、设置监视、评估变量、 [查看实际的 HTML DOM](viewing-actual-html-dom.html) 等。

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

