3

I'm trying to configure (Windows) Visual Studio Code launch.json to launch jest tests for the current file. To get the path I use ${relativeFile} variable which gives a string with backslashes like this "src\services\some-service.spec.ts", although in the documentation slashes look just normal.

It seems that jest doesn't accept this kind of path because of reversed slashed. When I manually pass the same path with normal slashes it works just fine.

The question is is there any way to reverse backslashes in VSCode path predefined variables like ${relativeFile} or maybe some workarounds?

Serg
  • 6,742
  • 4
  • 36
  • 54
  • I found it [on github](https://github.com/Microsoft/vscode/issues/40288). Sorry, I didn't see, that it is still unsolved. – Joey Mar 15 '19 at 21:00
  • I saw that thread and I had no idea where he found that example, probably it was just an idea. – Serg Mar 15 '19 at 21:01

3 Answers3

7

[Update] 2020-05-01

Now jest cli supports option --runTestsByPath, so that you can explicitly specify a file path rather than a pattern, which allows you to use \ on windows.

So the following launch.json should work:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Jest Current File",
      "type": "node",
      "request": "launch",
      "args": [
        "node_modules/jest/bin/jest.js",
        "--runInBand",
        "--runTestsByPath",
        "${relativeFile}"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "cwd": "${workspaceFolder}"
    }
  ]
}

The following is the original answer:


It seems that jest doesn't plan to work with \ and vscode doesn't plan to provide features to replace chars in Predefined variables.

But there are some workarounds:

  1. use ${fileBasename} instead of ${relativeFile}

  2. Or use input variables so that vscode prompt you to input custom test name when you debug.

Here is an example launch.json for the above two workarounds.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Jest Current FileBaseName",
      "type": "node",
      "request": "launch",
      "args": [
        "node_modules/jest/bin/jest.js",
        "--runInBand",
        "${fileBasename}"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "cwd": "${workspaceRoot}"
    },
    {
      "name": "Jest Custom",
      "type": "node",
      "request": "launch",
      "args": [
        "node_modules/jest/bin/jest.js",
        "--runInBand",
        "${input:testName}"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "cwd": "${workspaceRoot}"
    }
  ],
  "inputs": [
    {
      "type": "promptString",
      "id": "testName",
      "description": "The file or name you want to test",
      "default": "${fileBaseName}"
    }
  ]
}
Equal
  • 354
  • 3
  • 9
2

Use variable extension.commandvariable.file.relativeFilePosix from extension command-variable where you need ${relativeFile} with forward slashes. There are also other useful substitutions in this extension.

Xubor
  • 54
  • 1
  • 3
  • Using VS Code to run jest tests through the debugger required forward slashes, this is the only fix that works. Thank you so much! – Dave Sep 27 '20 at 00:41
  • 1
    Doesn't work; it changes paths to be like `/c/folder/file.txt` instead of `c:/folder/file.txt`. – BrainSlugs83 Feb 20 '22 at 23:10
0

This problem is very similar to one I gave an answer to elsewhere. To summarize the explanation there:

  1. Instead of directly running node ..., run bash -c node ... so you can use shell command substitution in the command line.
  2. Pass the path through the cygpath -m program to get forward slashes, something like $(cygpath -m ${relativeFile}).

See the linked answer for more details and examples.

Scott McPeak
  • 8,803
  • 2
  • 40
  • 79
  • This answer saved my day. I was going with the fileBasenameNoExtension approach. Then I created a monorepo with common naming and that broke my ability to debug one file from anywhere. Thanks! – Michael P. Scott Jan 29 '21 at 23:53