Skip to content

Commit bc39bcc

Browse files
connor4312sindresorhusnovemberborn
authoredJul 27, 2020
Detect active inspectors; update VSCode debugging recipe
Don't open inspectors if one is already connected. Like with AVA's own `debug` command, disable timeouts and switch to verbose output. This allows VSCode 1.47 to debug AVA test files. Update VSCode debugging recipe accordingly. Promote Debug Terminal in favor of launch configuration. Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com> Co-authored-by: Mark Wubben <mark@novemberborn.net>
1 parent 142363b commit bc39bcc

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed
 

‎docs/recipes/debugging-with-vscode.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/do
44

55
You can debug your tests using [Visual Studio Code](https://code.visualstudio.com/).
66

7+
## Debugging with the debug terminal
8+
9+
You can use VS Code's “JavaScript Debug Terminal” to automatically debug AVA run on the command-line.
10+
11+
1. From the Command Palette (<kbd>F1</kbd> or <kbd>command + shift + p</kbd> / <kbd>control + shift + p</kbd>), run `Debug: Create JavaScript Debug Terminal`
12+
2. Run `npx ava` in the terminal
13+
714
## Creating a launch configuration
815

16+
Alternatively you can create a lanch configuration, which makes it easier to debug individual test files.
17+
918
1. Open a workspace for your project.
1019
1. In the sidebar click the *Debug* handle.
1120
1. Create a `launch.json` file.
@@ -19,25 +28,22 @@ You can debug your tests using [Visual Studio Code](https://code.visualstudio.co
1928
"name": "Debug AVA test file",
2029
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ava",
2130
"runtimeArgs": [
22-
"debug",
23-
"--break",
2431
"${file}"
2532
],
26-
"port": 9229,
2733
"outputCapture": "std",
2834
"skipFiles": [
2935
"<node_internals>/**/*.js"
3036
]
3137
}
3238
```
3339

34-
## Using the debugger
40+
### Using the debugger
3541

3642
Open the file(s) you want to debug. You can set breakpoints or use the `debugger` keyword.
3743

3844
Now, *with a test file open*, from the *Debug* menu run the *Debug AVA test file* configuration.
3945

40-
## Debugging precompiled tests
46+
### Debugging precompiled tests
4147

4248
If you compile your test files into a different directory, and run the tests *from* that directory, the above configuration won't work.
4349

@@ -51,11 +57,8 @@ Assuming the names of your test files are unique you could try the following con
5157
"name": "Debug AVA test file",
5258
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ava",
5359
"runtimeArgs": [
54-
"debug",
55-
"--break",
5660
"build/**/${fileBasenameNoExtension}.*"
5761
],
58-
"port": 9229,
5962
"outputCapture": "std",
6063
"skipFiles": [
6164
"<node_internals>/**/*.js"
@@ -65,7 +68,13 @@ Assuming the names of your test files are unique you could try the following con
6568

6669
## Serial debugging
6770

68-
By default AVA runs tests concurrently. This may complicate debugging. Add a configuration with the `--serial` argument so AVA runs only one test at a time:
71+
By default AVA runs tests concurrently. This may complicate debugging. Instead make sure AVA runs only one test at a time.
72+
73+
*Note that, if your tests aren't properly isolated, certain test failures may not appear when running the tests serially.*
74+
75+
If you use the debug terminal make sure to invoke AVA with `npx ava --serial`.
76+
77+
Or, if you're using a launch configuration, add the `--serial` argument:
6978

7079
```json
7180
{
@@ -74,17 +83,12 @@ By default AVA runs tests concurrently. This may complicate debugging. Add a con
7483
"name": "Debug AVA test file",
7584
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ava",
7685
"runtimeArgs": [
77-
"debug",
78-
"--break",
7986
"--serial",
8087
"${file}"
8188
],
82-
"port": 9229,
8389
"outputCapture": "std",
8490
"skipFiles": [
8591
"<node_internals>/**/*.js"
8692
]
8793
}
8894
```
89-
90-
*Note that, if your tests aren't properly isolated, certain test failures may not appear when running the tests serially.*

‎lib/api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class Api extends Emittery {
147147
runStatus = new RunStatus(selectedFiles.length, null);
148148
}
149149

150-
const debugWithoutSpecificFile = Boolean(this.options.debug) && selectedFiles.length !== 1;
150+
const debugWithoutSpecificFile = Boolean(this.options.debug) && !this.options.debug.active && selectedFiles.length !== 1;
151151

152152
await this.emit('run', {
153153
bailWithoutReporting: debugWithoutSpecificFile,

‎lib/cli.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,19 @@ exports.run = async () => { // eslint-disable-line complexity
8888
confError = error;
8989
}
9090

91-
let debug = null;
91+
// Enter debug mode if the main process is being inspected. This assumes the
92+
// worker processes are automatically inspected, too. It is not necessary to
93+
// run AVA with the debug command, though it's allowed.
94+
const activeInspector = require('inspector').url() !== undefined; // eslint-disable-line node/no-unsupported-features/node-builtins
95+
let debug = activeInspector ?
96+
{
97+
active: true,
98+
break: false,
99+
files: [],
100+
host: undefined,
101+
port: undefined
102+
} : null;
103+
92104
let resetCache = false;
93105
const {argv} = yargs
94106
.parserConfiguration({
@@ -122,7 +134,11 @@ exports.run = async () => { // eslint-disable-line complexity
122134
array: true,
123135
describe: 'Glob patterns to select what test files to run. Leave empty if you want AVA to run all test files instead. Add a colon and specify line numbers of specific tests to run',
124136
type: 'string'
125-
}))
137+
}), argv => {
138+
if (activeInspector) {
139+
debug.files = argv.pattern || [];
140+
}
141+
})
126142
.command(
127143
'debug [<pattern>...]',
128144
'Activate Node.js inspector and run a single test file',
@@ -148,6 +164,7 @@ exports.run = async () => { // eslint-disable-line complexity
148164
}),
149165
argv => {
150166
debug = {
167+
active: activeInspector,
151168
break: argv.break === true,
152169
files: argv.pattern,
153170
host: argv.host,
@@ -444,14 +461,14 @@ exports.run = async () => { // eslint-disable-line complexity
444461
} else {
445462
let debugWithoutSpecificFile = false;
446463
api.on('run', plan => {
447-
if (plan.debug && plan.files.length !== 1) {
464+
if (debug !== null && plan.files.length !== 1) {
448465
debugWithoutSpecificFile = true;
449466
}
450467
});
451468

452469
const runStatus = await api.run({filter});
453470

454-
if (debugWithoutSpecificFile) {
471+
if (debugWithoutSpecificFile && !debug.active) {
455472
exit('Provide the path to the test file you wish to debug');
456473
return;
457474
}

‎lib/worker/subprocess.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,14 @@ ipc.options.then(async options => {
205205
// to make sure we also track dependencies with custom require hooks
206206
dependencyTracking.install(testPath);
207207

208-
if (options.debug) {
209-
require('inspector').open(options.debug.port, options.debug.host, true); // eslint-disable-line node/no-unsupported-features/node-builtins
208+
if (options.debug && options.debug.port !== undefined && options.debug.host !== undefined) {
209+
// If an inspector was active when the main process started, and is
210+
// already active for the worker process, do not open a new one.
211+
const inspector = require('inspector'); // eslint-disable-line node/no-unsupported-features/node-builtins
212+
if (!options.debug.active || inspector.url() === undefined) {
213+
inspector.open(options.debug.port, options.debug.host, true);
214+
}
215+
210216
if (options.debug.break) {
211217
debugger; // eslint-disable-line no-debugger
212218
}

0 commit comments

Comments
 (0)
Please sign in to comment.