I want to use VS Code to edit and debug Cypress tests. It seems like this should be simple; the cypress docs mention VS Code directly (but give no clues about how to configure
@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:
npm i cross-env
.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.
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",
],
},
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.npm run cypr
(or whatever you called your npm script)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}"
}