question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I debug tests in Visual Studio Code?

See original GitHub issue

I’m trying to debug tests in Visual Studio Code, but I always receive an error like the following:

import * as GalleriesHttpPort from "./GalleriesHttpPort";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

How can I properly debug in Visual Studio Code?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zlanichcommented, Sep 11, 2020

I’ve also been trying tirelessly to get this working. Here are my finds so far:

Running serverless-bundle test --some-arg=foo does in fact pass the arg to jest. I tested this both by logging the argv in the serverless-bundle scripts that get passed to jest, as well as by trying to pass an unsupported arg to jest, upon which it threw an error:

Unrecognized CLI Parameter: 

Unrecognized option "P". Did you mean "$0"?

CLI Options Documentation:
  https://jestjs.io/docs/en/cli.html

I’m not sure however, how you would pass specific arguments to webpack or else-wise.

I was able to get VSCode Debugging w/ Breakpoints working in JS & Typescript files, though. Here’s my launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Serverless - Example Service",
      "cwd": "${workspaceRoot}/services/example-service",
      "runtimeExecutable": "node",
      "runtimeArgs": ["--lazy"],
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceRoot}/services/example-service/node_modules/.bin/serverless",
      "args": [
        "offline",
        "--stage=dev",
        "--noTimeout",
        "--dontPrintOutput",
        "-P",
        "4000",
      ],
      "sourceMaps": true,
      "smartStep": false,
      "stopOnEntry": false,
      "outFiles": [
        "${cwd}/.webpack/**/*"
      ],
      "protocol": "inspector",
      "env": {
        "NODE_ENV": "development",
        "SLS_DEBUG": "*",
      },
      "autoAttachChildProcesses": true,
      "windows": {
          "cwd": "${workspaceRoot}\\services\\example-service",
          "program": "${workspaceRoot}\\services\\example-service\\node_modules\\.bin\\serverless"
      }
    }
  ]
}

I also have a custom directive in serverless.yml, though I’m not 100% certain it’s needed anymore:

custom:
  serverless-offline:
    port: 4000 # This has to be the same as the launch.json port for debug

For now, I just have it running this one service, but I intend to make it more universal. I can successfully hit http://localhost:4000/someserverlessfunc and it hits breakpoints in all file types. It’s fickle sometimes and requires me to run it twice, and the dots are always grey because VSCode can’t make the connection between src/dist files in this case for some reason, but it works.

In serverless-bundle test, I’m unable to get VSCode to hit breakpoints in plain JS test files, JS src files, webpack-generated files or anything; not even with a debugger; statement. I’ve looked at the jest-starter above, but didn’t find it to be terribly helpful yet, and I’ve experimented with a LOT of launch.json params, based on my working debug config and possible changes I’ve found on the web. So far no luck.

My experience with webpack, etc isn’t terribly deep, so I’m not 100% certain where to go from here. Does anyone at least have an idea of where to go from here, or at least some knowledge of how the VSCode -> Jest/Babel relationship works for debug that might help me tinker more?

Thanks!

0reactions
girdharsonicommented, Dec 21, 2022

@zlanich i have tried your solution in windows, but didn’t work for me, is your solutions work for windows or other oprating system.

Anyone got any solution regarding it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Debugging in Visual Studio Code
To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and...
Read more >
Debug Tests in Visual Studio Code | Recipes | Docs - TestCafe
Before you debug in Visual Studio Code, ensure that your root test directory contains a package.json file that includes testcafe in the devDependencies...
Read more >
How to Use VS Code to Debug Unit Test Cases | by Jennifer Fu
Click the following Run button on the left side panel. It presents an interface for Run and Debug. By specifying a proper configuration,...
Read more >
Using Visual Studio Code to debug Jest based unit tests
We will click on the debug icon (left hand sidebar). Click on the add configuration option in the dropdown list. Choose nodejs (jest...
Read more >
VS Code: Debugging with Jest - gists · GitHub
Start debugging · Open the unit test file you want to debug. · Set breakpoints or the debugger statement where you want to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found