Debugging tests in WebStorm and create-react-app
TL;DR: Here is a GitHub repo that has a create-react-app project with necessary dev dependencies and the WebStorm run configurations. npm install and open the project in Webstorm. You can set a breakpoint in App.test.js and run the Tests configuration in debug mode to try it out.
(This has only been tested with Node.js 6.9+ and WebStorm 2016.3. Sorry if the project doesn’t load correctly — Webstorm can be finicky about .idea files)
While you can eject your project and take over the configuration, I’ve been trying to stay within the defaults and boundaries of create-react-app. So I was looking for a solution that wouldn’t require I eject the project.
I broke the problem up into two pieces:
- Getting ES6 (or ES7) code running in the WebStorm debugger
- Getting a test suite to run in the debugger
npm install --save-dev babel-cli babel-preset-es2015
Running Es6 Hello World In the WebStorm Debugger
The latest version of Node will run many ES6 features, but module imports/exports don’t work (I am referring to the ES6 syntax, not use of require). You can read about this (here, here, …here too), but the short version is the spec for handling imports isn’t finalized — so Node doesn’t support it yet. But most React samples, such as the ones in create-react-app, use imports.
Babel includes babel-node, which handles transpiling and can be used as the node interpreter in WebStorm, but without some tweaks, it won’t handle imports. I found a YouTube video which showed how to set things up.
In a WebStorm Run configuration (node.js)
- set babel-node as node interpreter
- add — presets=babel-preset-es2015 as a node parameter
You can then set a breakpoint in helloworld.js and run the configuration in debug mode in WebStorm. A minor victory. :)
Testing with create-react-app
The README helps you setup your tests and a bootstrapped project includes a sample test, App.test.js. But this uses the “magic” of react-scripts — how do i get this all to run in WebStorm?
- add — env=jsdom as an application parameter
- add CI=true as an environment variable (so all tests will run instead of running in watch mode)
The final config should look like this:
Set a breakpoint and debug away!
Ideally, you wouldn’t need to debug your tests, i.e. a combination of appropriate expects and precise tests would indicate the problem, but sometimes things aren’t clear and running in a debugger helps. Anyway, I like the option.
You can download this GitHub repo and try for yourself.