簡體   English   中英

在 Jest 中調試單個測試

[英]Debug a single test in Jest

在許多測試套件中,在許多測試文件中,我有許多笑話測試。

我需要隔離和調試單個測試。

我正在通過node --inspect-brk./node_modules/jest/bin/jest進行調試,因此涉及監視模式的其他解決方案過於復雜。

除了需要調試的測試之外,我如何跳過所有測試?

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() => {/*...*/})

我使用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
}]
}

如果你使用 VSCode,Jest 擴展可能會派上用場。

https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest

https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner

另一個擴展。 這個使用 CodeLens,因此每個測試上方都有 Run|Debug 鏈接:

開玩笑的跑步者單元測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM