I want to use VS Code to edit and debug Cypress tests. The cypress docs mention VS Code directly, but don't give any clues about how to configure VS Code's launch.json
file for debugging either there or on the debugging page.
I have a launch.json
configuration that starts cypress/electron, but VS Code gives this error:
Cannot connect to runtime process… connect ECONNREFUSED 127.0.0.1:5858
Then shuts it down.
Looking at the sample electron for VS Code project doesn't help, and adding protocol
or program
attributes didn't work.
Here is my configuration:
{
"name": "Start integration_tests",
"type": "node",
"request": "launch",
"stopOnEntry": false,
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/cypress",
"runtimeArgs": [
"open"
],
"console" : "internalConsole",
"port": 5858,
}
Cypress provides options to use the native debugger keyword in tests. Just put the debugger keyword at the juncture in which test execution should pause. In the above example the debugger is deployed in the second it(). When the test runs, it will pause as soon as it encounters the debugger keyword.
IntelliJ Platform Compatible with IntelliJ IDEA, AppCode, CLion, GoLand, PhpStorm, PyCharm, Rider, RubyMine, and WebStorm. Cypress Support: Integrates Cypress under the common Intellij test framework.
Your Cypress test code runs in the same run loop as your application. This means you have access to the code running on the page, as well as the things the browser makes available to you, like document, window, and debugger. Based on those statements, you might be tempted to throw a debugger into your test, like so:
Put the word debugger in your test. See Cypress Doc on debugging. Or, if you are confident in your source maps, put a breakpoint in your code with vscode. Run npm run cypr (or whatever you called your npm script)
Cypress provides its default folder hierarchy, which makes the test development quick and easy. Moreover, Cypress uses Mocha's BDD constructs for the development of test cases. We can invoke Cypress methods using the "cy" object. Cypress provides a "visit" method to browse any webpage.
The documented way of testing an extension will run another instance of vscode and execute tests on it. But to be able to connect with Cypress we will need an endpoint we can hit. I decided to leverage a browser based vscode project called code-server. The easiest way to run it is with a docker image ( codercom/code-server:latest) .
@fhilton's answer used to work, but with newer versions of Cypress, it will cause Chrome to not be able to connect to the test runner and not run any tests. Use this instead:
If you or any of your co-workers develop in Windows, run npm i -D cross-env
.
In package.json add a script to start the Cypress test runner (or if you already have a script that says something like cypress open
then just modify that). You want the script to set the CYPRESS_REMOTE_DEBUGGING_PORT
environment variable to something like 9222
before it runs cypress open
. Since I use Windows, I use the cross-env
npm package to set environment variables. Therefore, the script in my package.json looks like
"scripts": {
"cypr": "cross-env CYPRESS_REMOTE_DEBUGGING_PORT=9222 cypress open",
},
I got the idea of doing that from here and here. The rest of this answer is mostly what @fhilton wrote in his answer so most of the credit goes to them.
Add the following to the list of configurations
in your launch.json (note the same port as above)
{
"type": "chrome",
"request": "attach",
"name": "Attach to Cypress Chrome",
"port": 9222,
"urlFilter": "http://localhost*",
"webRoot": "${workspaceFolder}",
"sourceMaps": true,
"skipFiles": [
"cypress_runner.js",
],
},
Put the word debugger
in your test. See Cypress Doc on debugging. Or, if you are confident in your source maps, put a breakpoint in your code with vscode.
Run npm run cypr
(or whatever you called your npm script)
From the Cypress test runner, start your tests running in Chrome
Start the vscode debugger with your new "Attach to Cypress Chrome" configuration
Restart the test with breakpoint in it and debug away!
I set this up today and it worked!
module.exports = (on, config) => {
on('before:browser:launch', (browser = {}, args) => {
if (browser.name === 'chrome') {
args.push('--remote-debugging-port=9222')
// whatever you return here becomes the new args
return args
}
})
}
Cypress Browser Launch API
{
"type": "chrome",
"request": "attach",
"name": "Attach to Chrome",
"port": 9222,
"urlFilter": "http://localhost:4200/*",
"webRoot": "${workspaceFolder}"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With