|
| 1 | +# Debugging tests in VS Code |
| 2 | + |
| 3 | +by [Jag Reehal](https://twitter.com/jagreehal) |
| 4 | + |
| 5 | +This recipe shows how to use the built-in Node Debugger to debug [Jest](https://facebook.github.io/jest/) tests. |
| 6 | + |
| 7 | +## The example |
| 8 | + |
| 9 | +The test folder contains two files that test the lib/calc.js file. |
| 10 | + |
| 11 | +To try the example you'll need to install dependencies by running: |
| 12 | + |
| 13 | +`npm install` |
| 14 | + |
| 15 | +## Configure launch.json File for yur test framework |
| 16 | + |
| 17 | +* Click on the Debugging icon in the Activity Bar to bring up the Debug view. |
| 18 | + Then click on the gear icon to configure a launch.json file, selecting **Node** for the environment: |
| 19 | + |
| 20 | +* Replace content of the generated launch.json with the following configurations: |
| 21 | + |
| 22 | +```json |
| 23 | +{ |
| 24 | + "version": "0.2.0", |
| 25 | + "configurations": [ |
| 26 | + { |
| 27 | + "type": "node", |
| 28 | + "request": "launch", |
| 29 | + "name": "Jest All", |
| 30 | + "program": "${workspaceRoot}/node_modules/jest/bin/jest", |
| 31 | + "args": ["--runInBand"], |
| 32 | + "console": "integratedTerminal", |
| 33 | + "internalConsoleOptions": "neverOpen" |
| 34 | + }, |
| 35 | + { |
| 36 | + "type": "node", |
| 37 | + "request": "launch", |
| 38 | + "name": "Jest Current File", |
| 39 | + "program": "${workspaceRoot}/node_modules/jest/bin/jest", |
| 40 | + "args": ["${file}"], |
| 41 | + "console": "integratedTerminal", |
| 42 | + "internalConsoleOptions": "neverOpen" |
| 43 | + } |
| 44 | + ] |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Debugging all tests |
| 49 | + |
| 50 | +You can debug all tests by following the steps below: |
| 51 | + |
| 52 | +1. Set a breakpoint in a test file or files |
| 53 | + |
| 54 | +2. Go to the Debug view, select the **'Jest All'** configuration, then press F5 or click the green play button. |
| 55 | + |
| 56 | +3. Your breakpoint will now be hit |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +## Debugging the current test |
| 61 | + |
| 62 | +You can debug the test you're editing by following the steps below: |
| 63 | + |
| 64 | +1. Set a breakpoint in a test file |
| 65 | + |
| 66 | +2. Go to the Debug view, select the **'Jest Current File'** configuration, then press F5 or click the green play button. |
| 67 | + |
| 68 | +3. Your breakpoint will now be hit |
| 69 | + |
| 70 | + |
0 commit comments