From b555192b43876c423a81f0cd699f3f4a94aa7441 Mon Sep 17 00:00:00 2001 From: Matt Morgis Date: Tue, 14 Nov 2017 08:05:34 -0500 Subject: [PATCH] Explain how to debug tests (#2992) * docs: adding section about debugging tests * docs: removing node-inspector references * docs: replacing terminal command with npm script * Update README.md --- packages/react-scripts/template/README.md | 63 +++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/packages/react-scripts/template/README.md b/packages/react-scripts/template/README.md index b8c0b74df39..2aa6648839a 100644 --- a/packages/react-scripts/template/README.md +++ b/packages/react-scripts/template/README.md @@ -64,6 +64,7 @@ You can find the most recent version of this guide [here](https://github.com/fac - [Disabling jsdom](#disabling-jsdom) - [Snapshot Testing](#snapshot-testing) - [Editor Integration](#editor-integration) +- [Debugging Tests](#debugging-tests) - [Developing Components in Isolation](#developing-components-in-isolation) - [Getting Started with Storybook](#getting-started-with-storybook) - [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 ![VS Code Jest Preview](https://cloud.githubusercontent.com/assets/49038/20795349/a032308a-b7c8-11e6-9b34-7eeac781003f.png) +## Debugging Tests + +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/). + +>Note: debugging tests requires Node 8 or higher. + +### Debugging Tests in Chrome + +Add the following to the `scripts` section in your project's `package.json` +```json +"scripts": { + "test:debug": "react-scripts --inspect-brk test --runInBand --env=jsdom" + } +``` +Place `debugger;` statements in any test and run: +```bash +$ npm run test:debug +``` + +This will start running your Jest tests, but pause before executing to allow a debugger to attach to the process. + +Open the following in Chrome +``` +about:inspect +``` + +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. + +>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. + +### Debugging Tests in Visual Studio Code + +Debugging Jest tests is supported out of the box for [Visual Studio Code](https://code.visualstudio.com). + +Use the following [`launch.json`](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations) configuration file: +``` +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug CRA Tests", + "type": "node", + "request": "launch", + "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts", + "runtimeArgs": [ + "--inspect-brk", + "test" + ], + "args": [ + "--runInBand", + "--no-cache", + "--env=jsdom" + ], + "cwd": "${workspaceRoot}", + "protocol": "inspector", + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen" + } + ] +} +``` + ## Developing Components in Isolation Usually, in an app, you have a lot of UI components, and each of them has many different states.