3
votes

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?

3
I found it on github. Sorry, I didn't see, that it is still unsolved.Joey
I saw that thread and I had no idea where he found that example, probably it was just an idea.Sergey

3 Answers

5
votes

[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}"
    }
  ]
}
2
votes

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.

0
votes

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.