【问题标题】:Debug a single test in Jest在 Jest 中调试单个测试
【发布时间】:2018-11-02 23:58:31
【问题描述】:

我有很多笑话测试,在很多测试套件中,在很多测试文件中。

我需要隔离和调试单个测试。

我是通过node --inspect-brk ./node_modules/jest/bin/jest调试的,所以其他涉及watch模式的解决方案太复杂了。

如何跳过除需要调试的测试之外的所有测试?

【问题讨论】:

  • 我澄清了这个问题以区别于其他问题——这个问题是关于调试的,并且需要一个与node --inspect-brk 调试方法兼容的解决方案

标签: javascript testing jestjs


【解决方案1】:

jest 分两步处理这个问题

  1. 通过使用 testPathPattern (jest docs) 命令行参数运行 jest 来隔离您的测试文件

    node --inspect-brk ./node_modules/jest/bin/jest --testPathPattern="integration.test"
    

    这里integration.test 是作为正则表达式提供的,用于过滤正确的测试文件

  2. 隔离您的测试功能
    有两种方法可以做到这一点

    • 一种方法是使用testNamePattern (jest docs) 命令行参数

      node --inspect-brk ./node_modules/jest/bin/jest --testNamePattern="host events"
      

      这里 host events 是作为正则表达式提供的,用于过滤正确的测试套件名称

    • 或者,您可以将.only 添加到您的测试函数中:

      • 这样test("object works", async() => {/*...*/})

      • 变成test.only("object works", async() => {/*...*/})

【讨论】:

    【解决方案2】:

    我正在使用 VSCode 进行调试,并在 launch.json 添加到 runTimeArgs 一个 testPathPattern 作为您的文件夹名称:

    {
    "version": "0.2.0",
    "configurations": [{
        "name": "Debug Jest Tests",
        "type": "node",
        "request": "launch",
        "runtimeArgs": [
            "--inspect-brk",
            "${workspaceRoot}/node_modules/jest/bin/jest.js",
            "--runInBand",
            "--testPathPattern=<NameOfTheFolder-you-want-to-test-without-this-angle-brackets>"
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen",
        "port": 9229
    }]
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-19
      • 2019-11-24
      • 2017-02-09
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2019-08-20
      相关资源
      最近更新 更多