简体   繁体   中英

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

Has anyone using start-server-and-test managed to debug their tests with breakpoints activated from within Visual Studio Code ? I am using start-server-and-test to start a docker-compose stack. Once the docker-compose stack is successfully started the Jest tests start to run.

I can run this successfully within a terminal session. However, I would like to trigger it from launch.json within VSCode if possible, with the ability to stop at breakpoints. I have tried with the following launch.json configuration item:

 {
            "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"
}

This starts the docker-compose stack and then the tests start running within an integrated terminal session in VSCode. However, no breakpoints are being hit within VSCode.

The following command is generated when VSCode runs the debugger:

/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"

If I try to run the e2e test script from within a terminal session the tests start running, as expected, after the docker-compose stack has successfully started:

# 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

Also tried this:

 {
            "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"
}

With this script:

"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'"

Same result...

Got it working in the end with this setup:

 {
            "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
}

My approach was to set the runtimeExecutable as start-server-and-test and initialise the runtimeArgs array with the arguments for start-server-and-test. The test argument for start-server-and-test spawns a node process with the --inspect-brk=9239 argument to allow the VSCode debugger to attach. Additionally this required explictly setting the port VSCode debugger option.

If child processes are spawned by end to end tests then will need to add the autoAttachChildProcesses option and ensure that the code that forks the child process detects if running in inspect mode. If it is then it will add inspect argument set with a different port to the parent so no conflict occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM