簡體   English   中英

如何在激活斷點的 Visual Studio Code 中調試 NodeJS start-server-and-test Jest 測試

[英]How to Debug NodeJS start-server-and-test Jest Tests in Visual Studio Code with Breakpoints Activating

是否有人使用start-server-and-test設法使用從Visual Studio Code中激活的斷點來調試他們的測試 我正在使用start-server-and-test來啟動docker-compose堆棧。 一旦 docker-compose 堆棧成功啟動,Jest 測試就會開始運行。

我可以在終端 session 中成功運行它。 但是,如果可能的話,我想從VSCode中的 launch.json 觸發它,並能夠在斷點處停止。 我嘗試了以下launch.json配置項:

 {
            "name": "Debug e2e tests",
            "type": "node",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "program": "${workspaceRoot}/node_modules/.bin/start-server-and-test",
            "args": [
                "docker-compose -f ./tests/docker/docker-compose.yml up",
                "http://localhost:8080/ping",
                "${workspaceRoot}/node_modules/.bin/jest -i --config=./tests/e2e/jest.config.js"
            ],
            "runtimeArgs": [
                "--inspect-brk"
            ],
            "protocol": "inspector",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
}

這將啟動 docker-compose 堆棧,然后測試開始在 VSCode 中的集成終端 session 中運行。 但是,在 VSCode 中沒有遇到斷點。

VSCode 運行調試器時會生成以下命令:

/usr/local/bin/node --inspect-brk=44745 node_modules/.bin/start-server-and-test "docker-compose -f ./tests/docker/docker-compose.yml up" http://localhost:8080/ping "/Users/simon/Development/Projects/ObjectDetection_Mqtt_Plugin/node_modules/.bin/jest -i --config=./tests/e2e/jest.config.js"

如果我嘗試從終端 session 中運行 e2e 測試腳本,則測試將在 docker-compose 堆棧成功啟動后按預期開始運行:

# package.json extract to include script for running e2e tests
"scripts": {
     "test:e2e": "start-server-and-test 'docker-compose -f ./tests/docker/docker-compose.yml up' http://localhost:8080/ping 'jest --config=./tests/e2e/jest.config.js'",
}

# run the e2e tests 
yarn run test:e2e

也試過這個:

 {
            "name": "Debug node script",
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "yarn",
            "runtimeArgs": [
                "run",
                "vscode"
            ],
            "restart": true,
            "port": 9229,
            "protocol": "inspector",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "disableOptimisticBPs": true,
            "internalConsoleOptions": "neverOpen"
}

使用此腳本:

"vscode": "start-server-and-test 'docker-compose -f ./tests/docker/docker-compose.yml up' http://localhost:8080/ping 'node --inspect-brk ./node_modules/.bin/jest --config=./tests/e2e/jest.config.js'"

結果一樣...

最終使用此設置使其正常工作:

 {
            "name": "Debug e2e tests",
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/start-server-and-test",
            "runtimeArgs": [
                "docker-compose -f ./tests/docker/docker-compose.yml up",
                "http://localhost:8080/ping",
                "node --inspect-brk=9239 --title=Jest-e2e-tests ${workspaceRoot}/node_modules/.bin/jest -i --config=./tests/e2e/jest.config.js"
            ],
            "port": 9239,
            "protocol": "inspector",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "disableOptimisticBPs": true
}

我的方法是將runtimeExecutable設置為 start-server-and-test 並使用 arguments 初始化runtimeArgs數組以進行 start-server-and-test。 start-server-and-test的 test 參數使用--inspect-brk=9239參數生成一個節點進程,以允許附加 VSCode 調試器。 此外,這需要明確設置port VSCode 調試器選項。

如果子進程是由端到端測試產生的,則需要添加autoAttachChildProcesses選項並確保派生子進程的代碼檢測是否在檢查模式下運行。 如果是,那么它將向父級添加具有不同端口的檢查參數集,因此不會發生沖突。

暫無
暫無

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

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