GoLand 2025.2 Help

测试 TypeScript

Starting with version 20, Node.js comes shipped with a stable version of the built-in Node.js test runner. GoLand 支持与内置测试运行器集成,您可以在无需安装和配置任何第三方框架的情况下运行测试。

GoLand provides out-of-the-box configuration for the ts-node and tsx loaders, so you can run and debug your TypeScript tests without previously compiling them into JavaScript. Alternatively, you can install a custom loader and configure it as described in Create a Node.js Test Runner run/debug configuration.

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

开始之前

  1. Make sure you have Node.js version 20 or later installed on your computer and configured as a local Node.js interpreter.

  2. 安装加载器。 在集成的 终端Alt+F12 )中,键入:

    • npm install --save-dev ts-node 安装 ts-node

    • npm install --save-dev tsx 安装 tsx

从编辑器运行测试

  • 要运行测试,请单击其边距旁的 运行测试图标 ,然后从列表中选择 运行 '<测试名称>'

    使用 Node.js 内置测试运行器从编辑器运行测试
  • 要运行套件中的所有测试,请单击其边距旁的 运行全部测试图标 ,然后从列表中选择 运行 '<套件名称>'

    使用 Node.js 内置测试运行器从编辑器运行测试套件

通过运行配置运行测试

通过运行/调试配置运行测试在需要从多个测试文件运行测试时非常有用。 使用运行/调试配置的另一个好处是可以配置自定义加载器。

Node.js Test Runner 运行/调试配置

You can create a run/debug configuration yourself or save, edit, and reuse a temporary run/debug configuration of the type Node.js 测试运行器 that GoLand creates when you run a test or a suite from the editor.

  1. 转至 运行 | 编辑配置。 或者,在工具栏的 运行 小组件中选择 编辑配置

    打开“编辑配置”对话框

    或者,选择先前生成的临时运行/调试配置。

  2. 在打开的 编辑配置 对话框中,单击工具栏上的 添加 按钮(添加按钮 ),然后从列表中选择 Node.js 测试运行器

    将打开 运行/调试配置:Node.js 测试运行器 对话框。

  3. 指定要使用的 Node.js 解释器、工作目录,并选择要运行的测试范围。

  4. 语言 列表中选择 TypeScript

  5. 加载器 列表中选择要使用的加载器。 By default, integration with tsx and ts -node is provided out-of-the-box.

    或者,安装并配置自定义加载器。 For example, install jiti and type -r jiti/register in the Node 选项 field.

    TypeScript 测试:配置自定义加载器
  6. 单击 运行

    或者,在 运行 小组件中选择一个已有的运行/调试配置,并单击其旁边的 调试图标

查看测试结果

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

测试结果

Learn more from Explore test results.

重新运行测试

  • 若要重新运行单个测试,请在 运行 工具窗口中选择该测试,然后从其上下文菜单中选择 运行 '<测试名称>' ,或按 Alt+Shift+R

    从“运行”工具窗口重新运行单个测试

    或者,使用测试旁边的边距图标进行重新运行。 单击 运行已忽略测试图标重新运行失败测试图标重新运行成功测试图标 以重新运行已忽略、失败或成功的测试,并从列表中选择 运行 '<测试名称>'

    从边距重新运行单个测试
  • 要重新运行整个套件,请单击套件旁边的标记图标,并从列表中选择 运行 '<套件名称>'

    从边距重新运行测试套件
  • 要重新运行上一会话中的所有测试,请单击 The Rerun '<suite name>' icon ,该按钮位于 运行 工具窗口的工具栏上。

    重新运行测试套件

调试测试

  1. 根据需要设置 断点

  2. 开始调试:

    • 要调试测试,请单击测试旁边标记中的 运行测试图标 ,然后从列表中选择 调试 '<测试名称>'

    • 要调试套件中的所有测试,请单击套件旁边标记中的 运行测试图标 ,然后从列表中选择 调试 '<套件名称>'

      TypeScript 测试:从编辑器调试测试套件
    • 或者,创建一种类型为 Node.js 测试运行器 如上所述 的运行/调试配置,从 运行 小部件中选择该配置,然后单击旁边的 调试图标

      TypeScript 测试:通过运行/调试配置调试测试
最后修改日期: 2025年 9月 26日