Skip to content

Commit f065bb6

Browse files
MattMorgisPavel Zhytko
authored and
Pavel Zhytko
committed
Explain how to debug tests (facebook#2992)
* docs: adding section about debugging tests * docs: removing node-inspector references * docs: replacing terminal command with npm script * Update README.md
1 parent 8482eb9 commit f065bb6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

packages/react-scripts/template/README.md

+63
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
6464
- [Disabling jsdom](#disabling-jsdom)
6565
- [Snapshot Testing](#snapshot-testing)
6666
- [Editor Integration](#editor-integration)
67+
- [Debugging Tests](#debugging-tests)
6768
- [Developing Components in Isolation](#developing-components-in-isolation)
6869
- [Getting Started with Storybook](#getting-started-with-storybook)
6970
- [Getting Started with Styleguidist](#getting-started-with-styleguidist)
@@ -1528,6 +1529,68 @@ If you use [Visual Studio Code](https://code.visualstudio.com), there is a [Jest
15281529
15291530
![VS Code Jest Preview](https://cloud.githubusercontent.com/assets/49038/20795349/a032308a-b7c8-11e6-9b34-7eeac781003f.png)
15301531
1532+
## Debugging Tests
1533+
1534+
There are various ways to setup a debugger for your Jest tests. We cover debugging in Chrome and [Visual Studio Code](https://code.visualstudio.com/).
1535+
1536+
>Note: debugging tests requires Node 8 or higher.
1537+
1538+
### Debugging Tests in Chrome
1539+
1540+
Add the following to the `scripts` section in your project's `package.json`
1541+
```json
1542+
"scripts": {
1543+
"test:debug": "react-scripts --inspect-brk test --runInBand --env=jsdom"
1544+
}
1545+
```
1546+
Place `debugger;` statements in any test and run:
1547+
```bash
1548+
$ npm run test:debug
1549+
```
1550+
1551+
This will start running your Jest tests, but pause before executing to allow a debugger to attach to the process.
1552+
1553+
Open the following in Chrome
1554+
```
1555+
about:inspect
1556+
```
1557+
1558+
After opening that link, the Chrome Developer Tools will be displayed. Select `inspect` on your process and a breakpoint will be set at the first line of the react script (this is done simply to give you time to open the developer tools and to prevent Jest from executing before you have time to do so). Click the button that looks like a "play" button in the upper right hand side of the screen to continue execution. When Jest executes the test that contains the debugger statement, execution will pause and you can examine the current scope and call stack.
1559+
1560+
>Note: the --runInBand cli option makes sure Jest runs test in the same process rather than spawning processes for individual tests. Normally Jest parallelizes test runs across processes but it is hard to debug many processes at the same time.
1561+
1562+
### Debugging Tests in Visual Studio Code
1563+
1564+
Debugging Jest tests is supported out of the box for [Visual Studio Code](https://code.visualstudio.com).
1565+
1566+
Use the following [`launch.json`](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations) configuration file:
1567+
```
1568+
{
1569+
"version": "0.2.0",
1570+
"configurations": [
1571+
{
1572+
"name": "Debug CRA Tests",
1573+
"type": "node",
1574+
"request": "launch",
1575+
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
1576+
"runtimeArgs": [
1577+
"--inspect-brk",
1578+
"test"
1579+
],
1580+
"args": [
1581+
"--runInBand",
1582+
"--no-cache",
1583+
"--env=jsdom"
1584+
],
1585+
"cwd": "${workspaceRoot}",
1586+
"protocol": "inspector",
1587+
"console": "integratedTerminal",
1588+
"internalConsoleOptions": "neverOpen"
1589+
}
1590+
]
1591+
}
1592+
```
1593+
15311594
## Developing Components in Isolation
15321595
15331596
Usually, in an app, you have a lot of UI components, and each of them has many different states.

0 commit comments

Comments
 (0)