Skip to content

pnpm run test -- is not handled as a package script when specified in jestCommandLine #974

@ermik

Description

@ermik

Environment

Jest 29 & PNPM 7

Prerequisite

  • are you able to run jest test from the command line? yes
  • how do you run your tests from the command line? pnpm run test

Steps to Reproduce

Set jestCommandLine to pnpm run test -- and use the debug test workflow.

Expected Behavior

The package script will be correctly delegated to for jest binary invocation.

Actual Behavior

The invocation will show node invoking the specified string expecting it to be a path to a module:

[...] /usr/local/bin/node ./pnpm run test

causing "Cannot find module" error. This works with npm run test because usage of a "magic string" (npm) changes the debug invocation to refer to the package manager instead of the local node binary.

Activity

connectdotz

connectdotz commented on Dec 23, 2022

@connectdotz
Collaborator

That's true, want to work on a PR? should be pretty straightforward:

private usePM(cmd: string, args: string[]): Partial<vscode.DebugConfiguration | undefined> {

ermik

ermik commented on Dec 26, 2022

@ermik
Author

Thanks for asking, I might. Will post here if I pick this up. 💟

linked a pull request that will close this issue on Jul 9, 2023
orpheus6678

orpheus6678 commented on Feb 7, 2025

@orpheus6678

I have faced the weirdest issue that might be related. I was using a Windows machine and in a Next.js project of mine I have Jest 29.7 configured manually, exactly as depicted in this article. If there was any key difference from a typical Next.js project, it is that I was using pnpm. I took care to set jestCommandLine to pnpm run test -- (package.json/scripts/test was set to "jest" of course) but if I did that, the command line built upon running from the extension is like

jest "--" "--testLocationInResults" "--json" "--useStderr" ...

and then Jest wouldnt find any of my tests and exit with error (testRegex would get 0 matches). I found out that the "--" in jest invocation was causing all this, what I did not expect was that setting jestCommandLine to pnpm test -- would fix this issue. It makes no sense, but then maybe this was the cause.

icy0307

icy0307 commented on May 19, 2025

@icy0307

vscode-jest/setup-wizard.md

Lines 127 to 133 in c93ec7a

The process goes like this:
- parse `jest.jestCommandLine` into command and arguments. Resolve relative command path based on `jest.rootPath` if defined otherwise uses vscode variable `"${workspaceFolder}"`.
- obtain a debug template from `DebugConfigurationProvider`
- merge the jest command and arguments with the template.
- if the command is `npm` or `yarn` it updates `runtimeExecutable` property; otherwise updates `program`
- the arguments will be added to the `args` property, plus jest debug specific flags such as `--runInBand`.
- update `cwd` property with either `jest.rootPath` if defined, otherwise the vscode variable `"${workspaceFolder}"`

According to setup-wizard "How is debug config generated" section, jest plugin updates runtimeExecutable property if the command is npm or yarn , otherwise updates program.
pnpm is not one of it , so it updates program to "program": "[project root]/node_modules/.bin/jest".

{
    "configurations": [
        {
            "type": "node",
            "name": "vscode-jest-tests.v2..leetcode",
            "request": "launch",
            "args": [
                "--runInBand",
                "--watchAll=false",
                "--testNamePattern",
                "${jest.testNamePattern}",
                "--runTestsByPath",
                "${jest.testFile}"
            ],
            "cwd": "/Users/bubu/.leetcode",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "disableOptimisticBPs": true,
            "program": "[project root]/node_modules/.bin/jest" // PNPM REWRITES IT TO SHELL BY DEFAULT
        }
    ]
}

However , the core problem is that pnpm creates the Jest binary file (node_modules/.bin/jest) as a shell script rather than a Node.js script, which cause issue like #868 . and this is also why #1046 would not work.

basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

User's own jestCommandLine might also not be node js as well, so the program field is not the answer. (scripts in package.json could also be shell scripts)
The alternatives' are

  1. make runtimeExecutable in package.json sh (But this probably would not work on windows, we need find out how to work on window)
  2. make runtimeExecutable pnpm
    Pnpm do have settings like prefer-symlinked-executables, but it is not default and might not be used on windows.
    so it would be nice to make this plugin support pnpm and other future package managers.
    @connectdotz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @ermik@connectdotz@icy0307@orpheus6678

      Issue actions

        `pnpm run test --` is not handled as a package script when specified in `jestCommandLine` · Issue #974 · jest-community/vscode-jest