Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: set up vscode config to attach to test:debug #4430

Merged
merged 2 commits into from Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "attach",
"name": "Attach to Karma test:debug",
"address": "localhost",
"port": 9765, // keep in sync with debugPort in karma.conf.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does .vscode not mind the comment in a json file?

This comment was marked as off-topic.

Copy link
Contributor Author

@dbjorge dbjorge Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it supports this. https://code.visualstudio.com/docs/languages/json#_json-with-comments

In addition to the default JSON mode following the JSON specification, VS Code also has a JSON with Comments (jsonc) mode. This mode is used for the VS Code configuration files such as settings.json, tasks.json, or launch.json.

"webRoot": "${workspaceFolder}",
"sourceMaps": true,
"sourceMapPathOverrides": {
"*": "${webRoot}/*",
"base/*": "${webRoot}/*"
}
}
]
}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Expand Up @@ -162,7 +162,7 @@ If you need to debug the unit tests in a browser, you can run:
npm run test:debug
```

This will start the Karma server and open up the Chrome browser. Click the `Debug` button to start debugging the tests. You can also navigate to the listed URL in your browser of choice to debug tests using that browser.
This will start the Karma server and open up the Chrome browser. Click the `Debug` button to start debugging the tests. You can either use that browser's debugger or attach an external debugger on port 9765; [a VS Code launch profile](./.vscode/launch.json) is provided. You can also navigate to the listed URL in your browser of choice to debug tests using that browser.

Because the amount of tests is so large, it's recommended to debug only a specific set of unit tests rather than the whole test suite. You can use the `testDirs` argument when using the debug command and pass a specific test directory. The test directory names are the same as those used for `test:unit:*`:

Expand Down
2 changes: 1 addition & 1 deletion doc/developer-guide.md
Expand Up @@ -71,7 +71,7 @@ There are also a set of tests that are not considered unit tests that you can ru

Additionally, you can [watch for changes](#watching-for-changes) to files and automatically run the relevant tests.

If you need to debug a test in a non-headless browser, you can run `npm run test:debug` which will run the Karma tests in non-headless Chrome. You can also navigate to the newly opened page using any supported browser.
If you need to debug a test in a non-headless browser, you can run `npm run test:debug` which will run the Karma tests in non-headless Chrome. You can either use that browser's debugger or attach an external debugger on port 9765; [a VS Code launch profile](../.vscode/launch.json) is provided. You can also navigate to the newly opened page using any supported browser.

You can scope which set of tests to debug by passing the `testDirs` argument. Supported values are:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -80,7 +80,7 @@
"test": "npm run test:tsc && run-s \"test:unit:* -- {@}\" --",
"test:tsc": "tsc",
"test:unit": "karma start test/karma.conf.js",
"test:debug": "npm run test:unit -- --no-single-run --browsers=Chrome",
"test:debug": "npm run test:unit -- --no-single-run --browsers=ChromeDebugging",
"test:unit:core": "npm run test:unit -- testDirs=core",
"test:unit:commons": "npm run test:unit -- testDirs=commons",
"test:unit:rule-matches": "npm run test:unit -- testDirs=rule-matches",
Expand Down
2 changes: 1 addition & 1 deletion test/integration/rules/README.md
Expand Up @@ -2,7 +2,7 @@

Rule Integration tests take an HTML snippet file and runs an axe-core rule against it. The results for the run are then compared against the companion JSON file to ensure that every node returns as the expected result (passes, violation, incomplete, inapplicable).

To run the rule integration tests, run `npm run test:unit:integration`. You can run and debug the tests in a non-headless browser by running `npm run test:debug -- testDirs=integration`.
To run the rule integration tests, run `npm run test:unit:integration`. You can run and debug the tests in a non-headless browser by running `npm run test:debug -- testDirs=integration`. You can either use that browser's debugger or attach an external debugger on port 9765; [a VS Code launch profile](../../../.vscode/launch.json) is provided.

When the tests are run, each JSON file is converted into a test suite file using [Karmas preprocessor](https://karma-runner.github.io/latest/config/preprocessors.html) and [runner.js](./runner.js) as the test suite template.

Expand Down
11 changes: 11 additions & 0 deletions test/karma.conf.js
Expand Up @@ -11,6 +11,7 @@ var testDirs = [
'virtual-rules'
];
var testFiles = [];
var debugPort = 9765; // arbitrary, sync with .vscode/launch.json
var args = process.argv.slice(2);

args.forEach(function (arg) {
Expand All @@ -23,6 +24,10 @@ args.forEach(function (arg) {
else if (parts[0] === 'testFiles') {
testFiles = parts[1].split(',');
}
// pattern: debugPort=1234
else if (parts[0] === 'debugPort') {
debugPort = parseInt(parts[1], 10);
}
});

var testPaths = [];
Expand Down Expand Up @@ -108,6 +113,12 @@ module.exports = function (config) {
timeout: 4000,
reporter: 'html'
}
},
customLaunchers: {
ChromeDebugging: {
base: 'Chrome',
flags: ['--remote-debugging-port=' + debugPort]
}
}
});
};