10

I am trying to configure Visual Studio Code to use TypeScript (in a Node package), I'm also using ts-jest/Jest to create tests, additionally, I have the Jest extension installed.

I have been able to figure out how to make breakpoints work, but now I notice I can't properly step in my code, I'm not even 100% sure the breakpoints work perfectly.

When clicking the step button, the execution is resumed and it never stops again on the next line, the only way to make it stop is to place other breakpoints where I want. The code does stop at the right place where the breakpoints have been set AFAICT.

EDIT: Actually, stepping sometimes stops again, but on what seems to be js code from implementation (for instance, console.log steps to code related to logging, but never steps again on my code)

EDIT #2: By trying to reproduce the issue with a simpler project, I realise not importing any more a library I'm using (Phaser) fixes the issue, I hope this can help isolate the cause of the issue.

EDIT #3: Here is a link to something that reproduces the issue I'm speaking about: https://github.com/adtrevor/VSCodeDebugIssue If you want to test it, just clone it, npm install and launch the "Debug Jest Tests" task from the run/debug panel. Set a breakpoint on line 23 of hey.ts (first instruction of barFunction()), the debugger will stop there, but if you step in the code, you should notice that at some point you leave current scope (after executing console.log("B") in my case) and never come back to it.

EDIT #4: Here is a more complete example showing the issue https://github.com/adtrevor/TSVSCodeSOQuestion/. If you clone it, install dependencies, and set a breakpoint to line 156 of ChunkManager.ts, you will notice stepping doesn't behave properly even though I applied the changes of @GiacomoDeLiberali. Maybe the bug is related, though.

I don't know at all what's wrong with the configuration or where to look first as I have no other error message regarding the debugging. Here is the content of my config files:

launch.json

{
  // Use IntelliSense to learn about possible Node.js debug attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Jest Tests",
      "cwd": "${workspaceFolder}",
      "args": [
        "--inspect-brk",
        "${workspaceRoot}/node_modules/.bin/jest",
        "--runInBand",
        "--config",
        "${workspaceRoot}/jest.config.js"
      ],
      "windows": {
        "args": [
          "--inspect-brk",
          "${workspaceRoot}/node_modules/jest/bin/jest.js",
          "--runInBand",
          "--config",
          "${workspaceRoot}/jest.config.json"          
        ],
      },
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
    },
  ]
}

settings.json

{
    "editor.fontSize": 14,
    "workbench.colorTheme": "Default Light+",
    "window.autoDetectColorScheme": true,
    "debug.node.autoAttach": "off",
    "jest.pathToConfig": "./jest.config.js",
}

jest.config.json

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  rootDir: './src/'
};

tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "CommonJS",
        "strict": true,
        "noImplicitAny": true,
        "allowJs": true,
        "jsx": "preserve",
        "importHelpers": true,
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "sourceMap": true,
        "baseUrl": "./src",
        "noEmit": false,
        "outDir": "./build/",

        "paths": {
          "~/*": ["./*"]
        },
        "typeRoots": [
            "node_modules/@types",
            "node_module/phaser/types"
        ],
        "types": [
            "phaser",
            "jest"
        ]
    },
    "include": [
        "src/**/*"
    ]
}

And finally my Package.json:

{
    "name": "mypackagename",
    "description": "Client for the game",
    "version": "0.1.0",
    "dependencies": {
        "phaser": "^3.22.0",
        "tslib": "^1.11.1"
    },
    "devDependencies": {
        "@types/jest": "^25.2.1",
        "bufferutil": "^4.0.1",
        "canvas": "^2.6.1",
        "jest": "^25.2.7",
        "ts-jest": "^25.3.1",
        "typescript": "^3.8.3",
        "utf-8-validate": "^5.0.2"
    }
}

Do you see anything unusual with this config? In general, what could be the cause of such a behaviour?

halfer
  • 18,701
  • 13
  • 79
  • 158
Trevör
  • 2,531
  • 1
  • 21
  • 55
  • Had you tried to inline the source maps? Maybe VS failed to load the source maps. (Within your compiler options for TS: "inlineSourceMap": true) – WolverinDEV Apr 14 '20 at 19:25
  • @WolverinDEV Thanks, I just tried it but unfortunately it doesn't fix the issue, the behaviour is still the same as what I described – Trevör Apr 14 '20 at 19:42
  • I don't see any prelaunch task to build your code. can you confirm that your code is built and up to date (along with your sourcemaps)? Can we see your build task? – pushkin Apr 14 '20 at 22:45
  • Have you tried `"disableOptimisticBPs": true` in your launch.json config? – Mike Patrick Apr 14 '20 at 22:56
  • @MikePatrick I just tried it but it didn't change anything unfortunately, I edited my question with a link to a minimal reproducer of the issue, seems like importing anything and using its types causes the issue – Trevör Apr 14 '20 at 23:22
  • @pushkin Sorry I had missed your comment, AFAICT `js-test` takes care of building the ts files to js as its specified as a `preset` in jest.config.json – Trevör Apr 15 '20 at 15:13
  • Please add these details to your question, OS, VS Code version, Node version – Tarun Lalwani Apr 16 '20 at 10:30
  • @Trevör does the `launch.json` patch fixed your debugging issue? – Giacomo De Liberali Apr 18 '20 at 17:53

1 Answers1

14

I've cloned the repo you posted, and it's a known issue of VS Code.

Try adding those lines inside your launch.json:

      "smartStep": true, 
      "skipFiles": [
        "<node_internals>/**",
        "node_modules/**"
      ]

smartStep: Automatically step through generated code that cannot be mapped back to the original source.

skipFiles: An array of glob patterns for files to skip when debugging.

I created a pull-request.

debug from VS Code

  • Thanks a lot for your answer and creating a pull-request to fix these kind of issues, unfortunately it didn't fix the issue for a more complex project I have, I thought the simplified version I posted on GitHub were a perfect reproduction of what I was experiencing but it seems there must be additional differences that still cause issues. Regardless of this thanks a lot for your help, it's still the biggest improvement I have seen to this issue :) – Trevör Apr 21 '20 at 13:46
  • Edited my question with a link to repo that still demonstrates the issue – Trevör Apr 21 '20 at 14:04
  • I cloned the updated repo and indeed vs code does stop on breakpoints, but doesn't navigate properly through the code. I'd try starting from a blank project to see when this issue kicks in. If I find something I'll let you know. – Giacomo De Liberali Apr 21 '20 at 15:43
  • I'm sorry, I see the bounty has been auto-awarded but was reduced by half, I didn't thought that was the case otherwise I would have awarded it as soon as the bounty period was finished. – Trevör Apr 23 '20 at 15:25