DEV Community

Cover image for Debug Jest Spec In VsCode - Error missing ) after argument list
Eyal Lapid
Eyal Lapid

Posted on

Debug Jest Spec In VsCode - Error missing ) after argument list

Photo by Ferenc Almasi on Unsplash

Scenario

Trying to debug a Jest spec in Vscode, leads to an error:

basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^
SyntaxError: missing ) after argument list
Enter fullscreen mode Exit fullscreen mode

Solution

There is a problem in the configuration. The default config tries to run the sh shell script of jest as a node program.

Instead, we can skip the sh jest script and run directly the jest nodejs script.

Instead of:

"${workspaceRoot}/node_modules/.bin/jest"
Enter fullscreen mode Exit fullscreen mode

We replace it with:

"${workspaceRoot}/node_modules/jest/bin/jest.js",
Enter fullscreen mode Exit fullscreen mode
   {
      "name": "vscode-jest-tests",
      "type": "node",
      "request": "launch",
      "runtimeArgs": [
        "--inspect-brk",
        "${workspaceRoot}/node_modules/jest/bin/jest.js",
        "--runInBand"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "port": 9229
    }
Enter fullscreen mode Exit fullscreen mode

Solution Walkthrough

In this case, I'm using pnpm. pnpm is creating a bin that is a bash script that calls node to run the actual bin nodejs script.

node_modules/.bin/jest

#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -z "$NODE_PATH" ]; then
  export NODE_PATH=".........."
else
  export NODE_PATH="$NODE_PATH:/........"
fi
if [ -x "$basedir/node" ]; then
  exec "$basedir/node"  "$basedir/../jest/bin/jest.js" "$@"
else
  exec node  "$basedir/../jest/bin/jest.js" "$@"
fi
Enter fullscreen mode Exit fullscreen mode

pnpm linkbin package

async function linkBin (cmd: CommandInfo, binsDir: string, opts?: { extendNodePath?: boolean }) {
  const externalBinPath = path.join(binsDir, cmd.name)

  try {
    let nodePath: string[] | undefined
    if (opts?.extendNodePath !== false) {
      nodePath = await getBinNodePaths(cmd.path)
      const binsParentDir = path.dirname(binsDir)
      if (path.relative(cmd.path, binsParentDir) !== '') {
        nodePath = union(nodePath, await getBinNodePaths(binsParentDir))
      }
    }
    await cmdShim(cmd.path, externalBinPath, {
      createPwshFile: cmd.makePowerShellShim,
      nodePath,
      nodeExecPath: cmd.nodeExecPath,
    })
  } catch (err: any) { // eslint-disable-line
    if (err.code !== 'ENOENT') {
      throw err
    }
    globalWarn(`Failed to create bin at ${externalBinPath}. The source file at ${cmd.path} does not exist.`)
    return
  }
Enter fullscreen mode Exit fullscreen mode

cmd-shim

  let sh = `\
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
case \`uname\` in
    *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac
Enter fullscreen mode Exit fullscreen mode

So the bin that the jest vscode launch configuration is targeting the bash script. The runner is set as a nodejs runner script. The script that it is trying to run is a sh script. That is the problem.

launch.json

   "type": "node",
    ...
    "${workspaceRoot}/node_modules/.bin/jest"
Enter fullscreen mode Exit fullscreen mode

We can find the atrribute for the launch.json here

So, instead of running the sh script, we redirect the launcher to run directly the jest nodejs script.

Top comments (3)

Collapse
 
ofhouse profile image
Felix Haus

Thanks for the hint!
For reference here is a full .vscode/launch.json file that can simply copy / pasted to make it work:

// .vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    // Default launch configuration for VSCode Jest extension
    {
      "name": "vscode-jest-tests",
      "type": "node",
      "request": "launch",
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
      "cwd": "${workspaceFolder}",
      "args": ["--runInBand", "--watchAll=false"]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ericlee33 profile image
Eric Lee

Thank you very much, i was stuck on this issue for long time

Collapse
 
itaigendler profile image
Itai Gendler

wow!
thank you very much!