title: VSCode: How to debug current Jest test file
body: <p>How to launch jest in debug mode on the current opened file, <strong><em>and only this one</em></strong>, regardless of the OS (windows, linux, mac).</p>
  
  <p><strong><em>The problem</em></strong>:</p>
  
  <p>When using vscode under windows, it is impossible to jest the current (active) file, and <strong>only this one</strong>.</p>
  
  <p>This debug config (from <a href="https://github.com/microsoft/vscode-recipes/tree/master/debugging-jest-tests" rel="noreferrer">https://github.com/microsoft/vscode-recipes/tree/master/debugging-jest-tests</a>):</p>
  
  <pre><code>{
        "type": "node",
        "request": "launch",
        "name": "Jest Current File",
        "program": "${workspaceFolder}/node_modules/.bin/jest",
        "args": [
          "${fileBasenameNoExtension}",
          "--config",
          "jest.config.js"
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen",
        "disableOptimisticBPs": true,
        "windows": {
          "program": "${workspaceFolder}/node_modules/jest/bin/jest",
        }
      }
  </code></pre>
  
  <p>Will <strong>not</strong> run <strong>only</strong> the active test if you have several tests with the same name (like index.test.js)</p>
  
  <p>If I replace</p>
  
  <pre><code>    "args": [
          "${fileBasenameNoExtension}",
          ....
      ]
  </code></pre>
  
  <p>by</p>
  
  <pre><code>    "args": [
          "${file}",
          ...
      ]
  </code></pre>
  
  <p>It <strong>will not work at all</strong> as the first argument jest expects is a regex and <code>${file}</code> returns the absolute path of the file, which, on a windows machine will contain <code>\</code> backslashes which in turn will be interpreted as escaping the following chars in the pattern.</p>
  
  <p>After reading carefully Jest's doc, it's impossible to specify a single file.</p>
  
  <p>And VSCode is unable to convert a variable to a regexp.</p>
  
  <p>The solution from <a href="https://stackoverflow.com/questions/55095712/jest-no-tests-found-after-update-vscode-to-1-32-1">this question</a>, although almost similar, will not work either as it fails with files of the same name (case 1).</p>
  
  <p>So either more tests than expected are run, or no test at all is run.</p>
  
  <p>I've found a solution involving a separate script but any other solution would be appreciated.</p>