create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Document how to debug unit tests

Open gaearon opened this issue 10 years ago • 52 comments

We should provide/document a way to do this: https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27. Support for this landed in Node 6.3.0.

gaearon avatar Sep 06 '16 15:09 gaearon

I can work on this.

wdhorton avatar Sep 06 '16 15:09 wdhorton

:+1:

I'm not sure if there's anything we need to expose here. I wonder how to pass flags to node when using npm scripts.

gaearon avatar Sep 06 '16 15:09 gaearon

@gaeron It's not pretty. @kentcdodds did something similar not so long ago.

geowarin avatar Sep 06 '16 15:09 geowarin

It's actually not terrible. The latest node 6 has --inspect and jest has documentation on how to debug with a browser.

kentcdodds avatar Sep 06 '16 15:09 kentcdodds

Haha, just noticed Dan already mentioned it's available in node 6 :sweat_smile:

kentcdodds avatar Sep 06 '16 15:09 kentcdodds

I mean node --inspect ./node_modules/.bin/jest is less ideal than jest -- --inspect or something similar 😄. And you need to test if node version is compatible, indeed.

But that would be a very cool feature. I've been looking for a good debugging tool for tests all around. What really works for me, at the moment, is WebStorm/IntelliJ + mocha (out-of-the-box support for a node script located in /node_modules/_mocha without sourcemap support). Native debugging in node is a big improvement.

geowarin avatar Sep 06 '16 23:09 geowarin

jest has documentation on how to debug with a browser

@kentcdodds I can't find the documentation on their site. Do you know where it is? It was announced in this June blog post, but the link doesn't go anywhere anymore

wdhorton avatar Sep 07 '16 03:09 wdhorton

Huh... That's weird... I can't find it anymore either. I wonder if @cpojer or @dmitriiabramov can explain why.

If I recall correctly, it was something like:

node --inspect --debug-brk ./node_modules/jest-cli --runInBand

kentcdodds avatar Sep 07 '16 08:09 kentcdodds

Yeah we removed the outdated docs for Jest that relied on node-inspector or other third party tools. Would appreciate a PR to Jest that adds docs on how to do this with node 6.3+.

cpojer avatar Sep 07 '16 12:09 cpojer

I mean node --inspect ./node_modules/.bin/jest is less ideal than jest -- --inspect or something similar 😄. And you need to test if node version is compatible, indeed.

But we can do exactly that automatically from scripts/test.js. So it could be a simple --debug flag on react-scripts side.

gaearon avatar Sep 07 '16 17:09 gaearon

So the idea is: From the user side, let them use something like react-scripts test --debug to trigger testing with debugging in browser. Add functionality to scripts/test to call the (potentially ugly) command node --inspect ./node_modules/.bin/jest ... when the --debug flag is passed?

I starting looking at this, and one problem I'm running into is that when I open the browser debugger, I can only seem to step through the code of jest itself, rather than my tests. Anyone experienced that before?

wdhorton avatar Sep 07 '16 21:09 wdhorton

You should probably run Jest with -i because otherwise Jest will run tests in worker processes.

cpojer avatar Sep 07 '16 21:09 cpojer

Trying to run it too, but no luck node --inspect --debug-brk ./node_modules/jest-cli/bin/jest.js -i --runInBand ./path-to-test.js and getting debugger stopped at jest-cli code here

> 'use strict';

if (process.env.NODE_ENV == null) {
  process.env.NODE_ENV = 'test';
}

require('../build/cli').run();

});

and when continue it ignores debugger statement inside my test

maximderbin avatar Sep 08 '16 21:09 maximderbin

node --inspect is still ignoring debugger statements, so I think we're going to have to wait until this Jest issue is resolved

wdhorton avatar Sep 15 '16 15:09 wdhorton

can I ask to take a look? http://stackoverflow.com/questions/39798495/how-to-debug-jest-unit-tests-on-create-react-app

asfktz avatar Oct 01 '16 11:10 asfktz

yikes. surprised this hasn't ~~come up before~~ had more heat applied to it. i see that react-scripts creates a jest config in mem. i wrote that config to disk, updated the targetEnvironment to jsdom instead of node, then ran:

node --debug-brk ./node_modules/.bin/jest --config jest-config.json --runInBand -i --no-cache src/__test__/<my-file>.test.js

and in fact it honored my debugger but it was super mangled (e.g. no source maps) :/

cdaringe avatar Dec 17 '16 00:12 cdaringe

can anyone furnish a successful workflow debugging via react-scripts with source maps?

cdaringe avatar Jan 06 '17 17:01 cdaringe

surprised this hasn't come up before had more heat applied to it.

You can apply some heat by submitting a PR. 😉

gaearon avatar Jan 06 '17 18:01 gaearon

i would love to do that once i get it to work! :)

cdaringe avatar Jan 06 '17 18:01 cdaringe

got it, PR comin' in hot!

cdaringe avatar Jan 07 '17 00:01 cdaringe