Skip to content

Commit 9f1374d

Browse files
authored
feat(testing): Unify VSCode debug config (#4117)
For a long time, we've had the following TODO in our VSCode `launch.json` (debug config) file: ``` // TODO: these are all alike save the package, so figure out how to make that variable ``` which was there because we had an ever-expanding number of nearly identical configurations, one per package. Each time we wanted to debug the tests in a new package, we had to copy and paste and make a new PR to get it on `master`. It was inefficient and frankly kind of a pain. When the need for yet another package's tests to be debugged came up, rather than continue the madness, I finally answered that TODO. This does a few things to make that happen: - Add an `inputs` section to the debug configuration, to allow more than the standard values in the config to be dynamic. - Include a command for reading the one part of the config that has been different per config entry - the package name - off of the open test file. - Use that command in the now-unified debug config. - Add the VSCode extension[1] which enables the input command to the list of recommended extensions, and a note about it to `launch.json`. - Add a "Debugging Tests" section to `CONTRIBUTING.md` explaining how to use the debugger. There's also a small amount of cleanup done to both the newly-unified config entry and the one other remaining one (which runs nextjs integration tests). [1] https://marketplace.visualstudio.com/items?itemName=augustocdias.tasks-shell-input
1 parent 8f51ef7 commit 9f1374d

File tree

3 files changed

+56
-113
lines changed

3 files changed

+56
-113
lines changed

.vscode/extensions.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
// See http://go.microsoft.com/fwlink/?LinkId=827846
33
// for the documentation about the extensions.json format
4-
"recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint"]
4+
"recommendations": [
5+
"esbenp.prettier-vscode",
6+
"dbaeumer.vscode-eslint",
7+
"augustocdias.tasks-shell-input"
8+
],
59
}

.vscode/launch.json

Lines changed: 38 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -3,150 +3,76 @@
33
// Hover to view descriptions of existing attributes.
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
6-
// TODO: these are all alike save the package, so figure out how to make that variable
6+
"inputs": [
7+
{
8+
"id": "getPackageName",
9+
"type": "command",
10+
"command": "shellCommand.execute",
11+
"args": {
12+
"command": "echo '${file}' | sed s/'.*sentry-javascript\\/packages\\/'// | grep --extended-regexp --only-matching --max-count 1 '[^\\/]+' | head -1",
13+
"cwd": "${workspaceFolder}" ,
14+
// normally `input` commands bring up a selector for the user, but given that there should only be one
15+
// choice here, this lets us skip the prompt
16+
"useSingleResult": true
17+
}
18+
}
19+
],
720
"configurations": [
8-
9-
// @sentry/core - run a specific test file in watch mode
10-
// must have file in currently active tab when hitting the play button
21+
// Run a specific test file in watch mode (must have file in currently active tab when hitting the play button).
22+
// NOTE: If you try to run this and VSCode complains that the command `shellCommand.execute` can't be found, go
23+
// install the recommended extension Tasks Shell Input.
1124
{
25+
"name": "Debug unit tests - just open file",
1226
"type": "node",
27+
"cwd": "${workspaceFolder}/packages/${input:getPackageName}",
1328
"request": "launch",
14-
"cwd": "${workspaceFolder}/packages/core",
15-
"name": "Debug @sentry/core tests - just open file",
1629
"program": "${workspaceFolder}/node_modules/.bin/jest",
1730
"args": [
1831
"--watch",
32+
// this makes the output less noisy (and at some point in the past seemed necessary to fix... something)
1933
"--runInBand",
34+
// TODO: when we unify jest config, we may need to change this
2035
"--config",
21-
"${workspaceFolder}/packages/core/package.json",
36+
"${workspaceFolder}/packages/${input:getPackageName}/package.json",
37+
// coverage messes up the source maps
2238
"--coverage",
23-
"false", // coverage messes up the source maps
24-
"${relativeFile}" // remove this to run all package tests
39+
"false",
40+
// remove this to run all package tests
41+
"${relativeFile}"
2542
],
26-
"disableOptimisticBPs": true,
2743
"sourceMaps": true,
2844
"smartStep": true,
29-
"console": "integratedTerminal", // otherwise it goes to the VSCode debug terminal, which prints the test output or console logs (depending on "outputCapture" option here), but not both
30-
"internalConsoleOptions": "neverOpen", // since we're not using it, don't automatically switch to it
45+
// otherwise it goes to the VSCode debug terminal, which prints the test output or console logs (depending on
46+
// "outputCapture" option here; default is to show console logs), but not both
47+
"console": "integratedTerminal",
48+
// since we're not using it, don't automatically switch to it
49+
"internalConsoleOptions": "neverOpen",
3150
},
3251

33-
// @sentry/nextjs - run a specific test file in watch mode
34-
// must have file in currently active tab when hitting the play button
35-
{
36-
"type": "node",
37-
"request": "launch",
38-
"cwd": "${workspaceFolder}/packages/nextjs",
39-
"name": "Debug @sentry/nextjs tests - just open file",
40-
"program": "${workspaceFolder}/node_modules/.bin/jest",
41-
"args": [
42-
"--watch",
43-
"--runInBand",
44-
"--config",
45-
"${workspaceFolder}/packages/nextjs/package.json",
46-
"--coverage",
47-
"false", // coverage messes up the source maps
48-
"${relativeFile}" // remove this to run all package tests
49-
],
50-
"disableOptimisticBPs": true,
51-
"sourceMaps": true,
52-
"smartStep": true,
53-
"console": "integratedTerminal", // otherwise it goes to the VSCode debug terminal, which prints the test output or console logs (depending on "outputCapture" option here), but not both
54-
"internalConsoleOptions": "neverOpen", // since we're not using it, don't automatically switch to it
55-
},
5652

57-
// @sentry/nextjs - run a specific integration test file
58-
// must have file in currently active tab when hitting the play button
53+
// @sentry/nextjs - Run a specific integration test file
54+
// Must have file in currently active tab when hitting the play button
5955
{
56+
"name": "Debug @sentry/nextjs integration tests - just open file",
6057
"type": "node",
61-
"request": "launch",
6258
"cwd": "${workspaceFolder}/packages/nextjs",
63-
"name": "Debug @sentry/nextjs integration tests - just open file",
59+
"request": "launch",
6460
// TODO create a build task
6561
// "preLaunchTask": "yarn build",
62+
63+
// this is going straight to `server.js` (rather than running the tests through yarn) in order to be able to skip
64+
// having to reinstall dependencies on every new test run
6665
"program": "${workspaceFolder}/packages/nextjs/test/integration/test/server.js",
6766
"args": [
6867
"--debug",
6968
// remove these two lines to run all integration tests
7069
"--filter",
7170
"${fileBasename}"
7271
],
73-
"disableOptimisticBPs": true,
7472
"sourceMaps": true,
7573
"skipFiles": [
7674
"<node_internals>/**", "**/tslib/**"
7775
],
7876
},
79-
80-
// @sentry/node - run a specific test file in watch mode
81-
// must have file in currently active tab when hitting the play button
82-
{
83-
"type": "node",
84-
"request": "launch",
85-
"cwd": "${workspaceFolder}/packages/node",
86-
"name": "Debug @sentry/node tests - just open file",
87-
"program": "${workspaceFolder}/node_modules/.bin/jest",
88-
"args": [
89-
"--watch",
90-
"--runInBand",
91-
"--config",
92-
"${workspaceFolder}/packages/node/package.json",
93-
"--coverage",
94-
"false", // coverage messes up the source maps
95-
"${relativeFile}" // remove this to run all package tests
96-
],
97-
"disableOptimisticBPs": true,
98-
"sourceMaps": true,
99-
"smartStep": true,
100-
"console": "integratedTerminal", // otherwise it goes to the VSCode debug terminal, which prints the test output or console logs (depending on "outputCapture" option here), but not both
101-
"internalConsoleOptions": "neverOpen", // since we're not using it, don't automatically switch to it
102-
},
103-
104-
// @sentry/tracing - run a specific test file in watch mode
105-
// must have file in currently active tab when hitting the play button
106-
{
107-
"type": "node",
108-
"request": "launch",
109-
"cwd": "${workspaceFolder}/packages/tracing",
110-
"name": "Debug @sentry/tracing tests - just open file",
111-
"program": "${workspaceFolder}/node_modules/.bin/jest",
112-
"args": [
113-
"--watch",
114-
"--runInBand",
115-
"--config",
116-
"${workspaceFolder}/packages/tracing/package.json",
117-
"--coverage",
118-
"false", // coverage messes up the source maps
119-
"${relativeFile}" // remove this to run all package tests
120-
],
121-
"disableOptimisticBPs": true,
122-
"sourceMaps": true,
123-
"smartStep": true,
124-
"console": "integratedTerminal", // otherwise it goes to the VSCode debug terminal, which prints the test output or console logs (depending on "outputCapture" option here), but not both
125-
"internalConsoleOptions": "neverOpen", // since we're not using it, don't automatically switch to it
126-
},
127-
128-
// @sentry/utils - run a specific test file in watch mode
129-
// must have file in currently active tab when hitting the play button
130-
{
131-
"type": "node",
132-
"request": "launch",
133-
"cwd": "${workspaceFolder}/packages/utils",
134-
"name": "Debug @sentry/utils tests - just open file",
135-
"program": "${workspaceFolder}/node_modules/.bin/jest",
136-
"args": [
137-
"--watch",
138-
"--runInBand",
139-
"--config",
140-
"${workspaceFolder}/packages/utils/package.json",
141-
"--coverage",
142-
"false", // coverage messes up the source maps
143-
"${relativeFile}" // remove this to run all package tests
144-
],
145-
"disableOptimisticBPs": true,
146-
"sourceMaps": true,
147-
"smartStep": true,
148-
"console": "integratedTerminal", // otherwise it goes to the VSCode debug terminal, which prints the test output or console logs (depending on "outputCapture" option here), but not both
149-
"internalConsoleOptions": "neverOpen", // since we're not using it, don't automatically switch to it
150-
},
15177
]
15278
}

CONTRIBUTING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ Running tests works the same way as building - running `yarn test` at the projec
4747

4848
Note: you must run `yarn build` before `yarn test` will work.
4949

50+
## Debugging Tests
51+
52+
If you run into trouble writing tests and need to debug one of them, you can do so using VSCode's debugger.
53+
54+
0. If you don't already have it installed, install the Tasks Shell Input extension, which you'll find in the Extensions tab in the sidebar as one of the recommended workspace extensions.
55+
56+
1. Place breakpoints or `debugger` statements in the test or the underlying code wherever you'd like `jest` to pause.
57+
2. Open the file containing the test in question, and make sure its tab is active (so you can see the file's contents).
58+
3. Switch to the debugger in the sidebar and choose `Debug unit tests - just open file` from the dropdown.
59+
4. Click the green "play" button to run the tests in the open file in watch mode.
60+
61+
Pro tip: If any of your breakpoints are in code run by multiple tests, and you run the whole test file, you'll land on those breakpoints over and over again, in the middle of tests you don't care about. To avoid this, replace the test's initial `it` or `test` with `it.only` or `test.only`. That way, when you hit a breakpoint, you'll know you got there are part of the buggy test.
62+
5063
## Linting
5164

5265
Similar to building and testing, linting can be done in the project root or in individual packages by calling `yarn lint`.

0 commit comments

Comments
 (0)