jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: Test environment resolves to wrong version when using `pnpm`

Open nicojs opened this issue 2 months ago • 6 comments

Version

30.2.0 and 24.9.0 inside a mono-repo

Steps to reproduce

git clone https://github.com/nicojs/jest-bug-pnpm-resolve
cd jest-bug-pnpm-resolve
pnpm install
cd test/jest-latest-version
node --run test

And see the error:

Expected behavior

 PASS  spec/math.spec.js
 PASS  spec/concat.spec.js

Test Suites: 2 passed, 2 total
Tests:       4 passed, 4 total

Actual behavior

Test environment found at "/Users/nicojs/repos/github/tmp/node_modules/.pnpm/[email protected]/node_modules/jest-environment-node/build/index.js" does not export a "getVmContext" method, which is mandatory from Jest 27. This method is a replacement for "runScript".

Additional context

The problem is with the current test environment resolve mechanism. It doesn't honor strict dependency resolve rules.

You can see it clearly by running jest --showConfig:

[...]
      "testEnvironment": "/Users/nicojs/repos/github/tmp/node_modules/.pnpm/[email protected]/node_modules/jest-environment-node/build/index.js
[...]

Note that it resolved to v24.9.0 instead of 30.2.0

npx --showConfig
{
  "configs": [
    {
      "automock": false,
      "cache": true,
      "cacheDirectory": "/private/var/folders/68/rr4tyh_53j74449j5tps_0ym0000gn/T/jest_dx",
      "clearMocks": false,
      "collectCoverageFrom": [],
      "coverageDirectory": "/Users/nicojs/repos/github/tmp/test/jest-latest-version/coverage",
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ],
      "coverageReporters": [
        "json",
        "text",
        "lcov",
        "clover"
      ],
      "cwd": "/Users/nicojs/repos/github/tmp/test/jest-latest-version",
      "detectLeaks": false,
      "detectOpenHandles": false,
      "errorOnDeprecated": false,
      "extensionsToTreatAsEsm": [],
      "fakeTimers": {
        "enableGlobally": false
      },
      "forceCoverageMatch": [],
      "globals": {},
      "haste": {
        "computeSha1": false,
        "enableSymlinks": false,
        "forceNodeFilesystemAPI": true,
        "throwOnModuleCollision": false
      },
      "id": "bd227e4e53075f54f93f4daa697c89b0",
      "injectGlobals": true,
      "moduleDirectories": [
        "node_modules"
      ],
      "moduleFileExtensions": [
        "js",
        "mjs",
        "cjs",
        "jsx",
        "ts",
        "mts",
        "cts",
        "tsx",
        "json",
        "node"
      ],
      "moduleNameMapper": [],
      "modulePathIgnorePatterns": [],
      "openHandlesTimeout": 1000,
      "prettierPath": "prettier",
      "resetMocks": false,
      "resetModules": false,
      "restoreMocks": false,
      "rootDir": "/Users/nicojs/repos/github/tmp/test/jest-latest-version",
      "roots": [
        "/Users/nicojs/repos/github/tmp/test/jest-latest-version"
      ],
      "runner": "/Users/nicojs/repos/github/tmp/node_modules/.pnpm/[email protected]/node_modules/jest-runner/build/index.js",
      "sandboxInjectedGlobals": [],
      "setupFiles": [],
      "setupFilesAfterEnv": [],
      "skipFilter": false,
      "slowTestThreshold": 5,
      "snapshotFormat": {
        "escapeString": false,
        "printBasicPrototype": false
      },
      "snapshotSerializers": [],
      "testEnvironment": "/Users/nicojs/repos/github/tmp/node_modules/.pnpm/[email protected]/node_modules/jest-environment-node/build/index.js",
      "testEnvironmentOptions": {},
      "testLocationInResults": false,
      "testMatch": [
        "/Users/nicojs/repos/github/tmp/test/jest-latest-version/spec/*.spec.js"
      ],
      "testPathIgnorePatterns": [
        "/node_modules/"
      ],
      "testRegex": [],
      "testRunner": "/Users/nicojs/repos/github/tmp/node_modules/.pnpm/[email protected]/node_modules/jest-circus/build/runner.js",
      "transform": [
        [
          "\\.[jt]sx?$",
          "/Users/nicojs/repos/github/tmp/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/babel-jest/build/index.js",
          {}
        ]
      ],
      "transformIgnorePatterns": [
        "/node_modules/",
        "\\.pnp\\.[^\\/]+$"
      ],
      "waitForUnhandledRejections": false,
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    "bail": 0,
    "changedFilesWithAncestor": false,
    "ci": false,
    "collectCoverage": false,
    "collectCoverageFrom": [],
    "coverageDirectory": "/Users/nicojs/repos/github/tmp/test/jest-latest-version/coverage",
    "coverageProvider": "babel",
    "coverageReporters": [
      "json",
      "text",
      "lcov",
      "clover"
    ],
    "detectLeaks": false,
    "detectOpenHandles": false,
    "errorOnDeprecated": false,
    "expand": false,
    "findRelatedTests": false,
    "forceExit": false,
    "json": false,
    "lastCommit": false,
    "listTests": false,
    "logHeapUsage": false,
    "maxConcurrency": 5,
    "maxWorkers": 13,
    "noStackTrace": false,
    "nonFlagArgs": [],
    "notify": false,
    "notifyMode": "failure-change",
    "onlyChanged": false,
    "onlyFailures": false,
    "openHandlesTimeout": 1000,
    "passWithNoTests": false,
    "projects": [],
    "rootDir": "/Users/nicojs/repos/github/tmp/test/jest-latest-version",
    "runInBand": false,
    "runTestsByPath": false,
    "seed": -2069774707,
    "skipFilter": false,
    "snapshotFormat": {
      "escapeString": false,
      "printBasicPrototype": false
    },
    "testFailureExitCode": 1,
    "testPathPatterns": [],
    "testSequencer": "/Users/nicojs/repos/github/tmp/node_modules/.pnpm/@[email protected]/node_modules/@jest/test-sequencer/build/index.js",
    "updateSnapshot": "new",
    "useStderr": false,
    "waitForUnhandledRejections": false,
    "watch": false,
    "watchAll": false,
    "watchman": true,
    "workerThreads": false
  },
  "version": "30.1.3"
}

Environment

System:
    OS: macOS 26.0.1
    CPU: (14) arm64 Apple M4 Pro
  Binaries:
    Node: 22.18.0 - /Users/nicojs/.nvm/versions/node/v22.18.0/bin/node
    Yarn: 1.22.22 - /Users/nicojs/.nvm/versions/node/v22.18.0/bin/yarn
    npm: 11.5.2 - /Users/nicojs/.nvm/versions/node/v22.18.0/bin/npm
    pnpm: 10.18.3 - /Users/nicojs/Library/pnpm/pnpm
  npmPackages:
    jest: ^30.2.0 => 30.2.0

nicojs avatar Oct 24 '25 08:10 nicojs

Found a workaround by installing jest-environment-node myself in the jest-latest-version package.

nicojs avatar Oct 24 '25 08:10 nicojs

@nicojs can you help verifying my PR able to fix your issue with the following patch? I can confirm it works on my own account but would like to acquire your result as well.

diff --git a/test/jest-latest-version/package.json b/test/jest-latest-version/package.json
index 644b6d1..34a5ab3 100644
--- a/test/jest-latest-version/package.json
+++ b/test/jest-latest-version/package.json
@@ -6,6 +6,6 @@
     "test": "NODE_OPTIONS=--experimental-vm-modules jest"
   },
   "dependencies": {
-    "jest": "^30.2.0"
+    "jest": "https://pkg.pr.new/jest@15877"
   }
 }

hainenber avatar Oct 26 '25 15:10 hainenber

Thanks for the quick response 🙏

This is strange. pnpm t now works, but node --run test doesn't. Is this also what you expected?

nicojs ~/repos/github/jest-bug-pnpm-resolve/test/jest-latest-version (main) $ node --run test
Test environment found at "/Users/nicojs/repos/github/jest-bug-pnpm-resolve/node_modules/.pnpm/[email protected]/node_modules/jest-environment-node/build/index.js" does not export a "getVmContext" method, which is mandatory from Jest 27. This method is a replacement for "runScript".

 RUNS  spec/math.spec.js
nicojs ~/repos/github/jest-bug-pnpm-resolve/test/jest-latest-version (main) $ pnpm t

> coverage-analysis@ test /Users/nicojs/repos/github/jest-bug-pnpm-resolve/test/jest-latest-version
> NODE_OPTIONS=--experimental-vm-modules jest

(node:75857) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 PASS  spec/math.spec.js
 PASS  spec/concat.spec.js

Test Suites: 2 passed, 2 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        0.168 s, estimated 1 s
Ran all test suites.

nicojs avatar Oct 27 '25 15:10 nicojs

I've pushed a new commit that handle node --run test in pnpm project, can you remove node_modules and rerun back to fetch newer packages?

hainenber avatar Oct 29 '25 14:10 hainenber

Sorry for the delay.

I still get the same issue. I've removed my pnpm-lock.yml file and node_modules and run pnpm i. I've committed it to the repo so you can validate my work.

 $ node --run test

Test environment found at "/Users/nicojs/repos/github/jest-bug-pnpm-resolve/node_modules/.pnpm/[email protected]/node_modules/jest-environment-node/build/index.js" does not export a "getVmContext" method, which is mandatory from Jest 27. This method is a replacement for "runScript".

 RUNS  spec/math.spec.js

nicojs avatar Nov 06 '25 17:11 nicojs

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Dec 06 '25 18:12 github-actions[bot]