Skip to content

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

@nicojs

Description

@nicojs

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/jest-environment-node@24.9.0/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/jest-environment-node@24.9.0/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/jest-runner@30.2.0/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/jest-environment-node@24.9.0/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/jest-circus@30.2.0/node_modules/jest-circus/build/runner.js",
      "transform": [
        [
          "\\.[jt]sx?$",
          "/Users/nicojs/repos/github/tmp/node_modules/.pnpm/babel-jest@30.2.0_@babel+core@7.28.5/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/@jest+test-sequencer@30.2.0/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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions