-
Notifications
You must be signed in to change notification settings - Fork 29.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Debugger Won't hit breakpoints in jest test #60187
Comments
(Experimental duplicate detection) |
In your webpack config, could you try changing |
Actually there is some discussion going on about the same issue in facebook/create-react-app#5319 |
I am going to keep this issue open to figure out a better workaround on vscode's end. Pasting my response from the other issue: tl;dr: a workaround is that breakpoints set after launching work. I can repro this, I tried downgrading jest in the test repo, but it doesn't make a difference for me? Could one of you share a test repo where it does work with jest downgraded? Or if it's easier just set "trace": true in your launch config, run it again, and share the log file. Here's the problem: In a transpiling scenario you can use the outFiles parameter to point vscode towards the output sourcemaps so it can preload them and resolve your breakpoints to their real locations. But in this scenario, the sourcemaps are generated in memory and don't exist on disk. So you set a breakpoint in a .js file. VS Code doesn't know whether it's a bp in a script with no sourcemaps, or a transpiled script that will have sourcemaps at runtime. Since it's a .js file, we guess and set breakpoints for that path and line. Anyone using .ts or something else won't see an issue. Then, jest loads the transpiled script and gives it the same path as the file on disk. It's a match, and your breakpoint hits. But the line numbers don't match between the two scripts, so you've stopped on the wrong line. At the same time, vscode has loaded the sourcemap so any breakpoints you set at this point will bind correctly. If this is really new in Jest then they might have changed how they name the loaded scripts. If they can name them differently from the source files on disk, that would fix this. Maybe we should provide an option to disable this optimistic bp placement but it should "just work"... But this scenario where sourcemaps can't be preloaded from disk has always been imperfect in vscode. We would still have a race to load the sourcemap and set the BP before the line of code with the BP executes. Chrome gives us a way to break every time a script loads, giving us a chance to catch up and load sourcemaps. Node doesn't support that unfortunately. We've experimented with guessing the name of the script at runtime, setting a BP for line 1, then checking whether it has sourcemaps. That would help in this scenario but is complex and relies on the name of the script at runtime matching the path on disk. One possibility is that we adapt |
Hey, Jest maintainer here 👋 We do write the source map to disk (as well as keep it inline in the sourcecode of the file, https://github.com/facebook/jest/blob/714cb516944c74a9ed9d6eda867911b398003468/packages/jest-runtime/src/script_transformer.js#L272). I'm not sure if it's possible to tell vs code where it is, though? |
Oh that's interesting, where is that path? |
It is in We write it to disk for caching purposes, but maybe you can use it? Is this something https://github.com/jest-community/vscode-jest can help solve? |
@roblourens I would very much like to have |
Hey @SimenB I can't locate the cache on my disk, and I can't tell whether that code is running for sure. Does this happen all the time by default or is there an option I have to set to enable it? I can't tell where |
Ah I did find it, never mind. But, since those files won't exist before we start running, this can't help us. Also since it's in a directory with random name, we won't know where to look for it. It does look like we can set the path by providing a config file. it would help if there's a command I can run that would write out the cache without running the tests. Then I can run that, preload the sourcemaps, then start debugging. Does something like that exist? |
Running |
Ok, I'm just not sure what to do about this then. When people use chrome devtools to debug, do they just rely on |
In chrome, you can't set breakpoints before a file is loaded, at which point the sourcemap is available. So it doesn't "guess" if I understand what you currently do correctly. Would it help if Jest exposed some |
Would that transpile the file on demand? I guess that would help but it would be simpler if there was one command to transpile the full project to a predictable location. Ideally we could prepare the project with a |
I added a flag that will make this work a little better. In the next Insiders build you can set Anyone watching this thread, I'd appreciate if you try it out in tomorrow's Insiders build. It's not a permanent solution but should unblock this at least. |
I'm still not clear on the workflow for debugging with chrome devtools. If people are used to adding debugger statements everywhere, then this isn't that bad. Really what we need is a way to pause when a script is loaded. Chrome gives us this, Node does not. Maybe an option to have Babel insert a debugger statement into each test would be helpful. |
(Settings in 1.30.0-insider doesn't seem to have _disableOpitimisticBPs) |
In your launch config (it will be underlined as unrecognized, it's undocumented) |
Has anyone tried this out? Can you comment on whether you it helps, and how the experience compares to using chrome devtools? Need to decide whether to make this part of the official recommended and documented workflow. |
Hey @roblourens , just tried this out and can confirm that it from my initial tests it is working perfectly, its stopping at the right breakpoints both in the I will continue to use the insiders build and will update this thread with any issues if they arrive but so far so good 😀 Thanks for the hard work on this, really helped me out. |
That's great, thanks for reporting back. |
Verification steps
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"disableOptimisticBPs": true,
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
|
I tried the verification steps by cloning the create-react-app repo, following the instructions to create an app, and then setting up the launch.json but I get an error when I try to debug: SyntaxError: missing ) after argument list |
@alexr00 it looks like you are running a linux/macOS shell script instead of a Windows script. Does it make a difference if you add a windows specific path to the launch config, like this: "windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
} (I'm trying to follow the recipe here: https://github.com/Microsoft/vscode-recipes/tree/master/debugging-jest-tests) |
So I don't understand why Also I updated |
@roblourens , thanks! Now breakpoints hits in correct places with using flag |
I also had to set my node-version to something more current. |
When running Jest tests, I am unable to set breakpoints in the *.test.js / *.spec.js files. When i set a breakpoint in a location and start the launch.json config the breakpoints move around.
I am unable to provide access to my repo so i have created a repo which is close to what i am working on, its an ejected create react app and can be seen here.
https://github.com/jacksaunders/vscode-jest-test
I ran vscode with all extensions disabled, and just ran the basic App test that comes with create react app.
here is a quick gif of what is happening
vscode version info
The text was updated successfully, but these errors were encountered: