2018-01-03 00:34:57 +03:00
# Debugging tests in VS Code
by [Jag Reehal ](https://twitter.com/jagreehal )
This recipe shows how to use the built-in Node Debugger to debug [Jest ](https://facebook.github.io/jest/ ) tests.
## The example
The test folder contains two files that test the lib/calc.js file.
To try the example you'll need to install dependencies by running:
`npm install`
2018-03-24 01:22:34 +03:00
## Configure launch.json File for your test framework
2018-01-03 00:34:57 +03:00
* Click on the Debugging icon in the Activity Bar to bring up the Debug view.
Then click on the gear icon to configure a launch.json file, selecting **Node** for the environment:
* Replace content of the generated launch.json with the following configurations:
```json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
2018-06-18 19:56:30 +03:00
"program": "${workspaceFolder}/node_modules/.bin/jest",
2018-01-03 00:34:57 +03:00
"args": ["--runInBand"],
"console": "integratedTerminal",
2018-06-22 02:03:17 +03:00
"internalConsoleOptions": "neverOpen",
2018-12-29 01:25:22 +03:00
"disableOptimisticBPs": true,
2018-06-22 02:03:17 +03:00
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
2018-01-03 00:34:57 +03:00
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
2018-06-18 19:56:30 +03:00
"program": "${workspaceFolder}/node_modules/.bin/jest",
2019-02-05 01:46:15 +03:00
"args": [
2019-09-20 20:50:10 +03:00
"--runTestsByPath",
"${relativeFile}",
2019-02-05 01:46:15 +03:00
"--config",
"jest.config.js"
],
2018-01-03 00:34:57 +03:00
"console": "integratedTerminal",
2018-06-22 02:03:17 +03:00
"internalConsoleOptions": "neverOpen",
2019-01-02 06:30:25 +03:00
"disableOptimisticBPs": true,
2018-06-22 02:03:17 +03:00
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
2018-01-03 00:34:57 +03:00
}
]
}
```
2018-07-30 18:34:33 +03:00
## Configure package.json File for your test framework
* Add following Jest configuration to package.json:
```
"jest": {
"testEnvironment": "node"
}
```
2018-06-22 02:03:17 +03:00
**Note for windows users** : if `node_modules/jest` is not available in your project, but `node_modules/jest-cli` is installed (e.g. if you are [using react-boilerplate ](https://github.com/react-boilerplate/react-boilerplate/blob/v3.6.0/package.json#L221 )) you can replace the windows attribute by this one for both launch configurations :
```json
"windows": {
"program": "${workspaceFolder}/node_modules/jest-cli/bin/jest",
}
```
2018-01-03 00:34:57 +03:00
## Debugging all tests
You can debug all tests by following the steps below:
2019-01-02 06:30:25 +03:00
1. Set a breakpoint in a test file or files.
2018-01-03 00:34:57 +03:00
2. Go to the Debug view, select the ** 'Jest All'** configuration, then press F5 or click the green play button.
2019-01-02 06:30:25 +03:00
3. Your breakpoint will now be hit.
> **Note**: Your breakpoint may not be hit on the first run. If it isn't hit, you can rerun the tests by pressing `a` at the prompt in the Terminal. Or, by adding a `debugger` statement to the top of the script (this just gives vscode time to process the script's sourcemaps when it is loaded).
2018-01-03 00:34:57 +03:00
![all ](all.gif )
## Debugging the current test
You can debug the test you're editing by following the steps below:
2019-01-02 06:30:25 +03:00
1. Set a breakpoint in a test file.
2018-01-03 00:34:57 +03:00
2. Go to the Debug view, select the ** 'Jest Current File'** configuration, then press F5 or click the green play button.
2019-01-02 06:30:25 +03:00
3. Your breakpoint will now be hit. (If not, see **Note** above).
2018-01-03 00:34:57 +03:00
![current ](current.gif )