Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Cannot find module '@jest/globals' when cache: false #4245

Open
nlwillia opened this issue Dec 3, 2023 · 1 comment
Open

[Bug]: Cannot find module '@jest/globals' when cache: false #4245

nlwillia opened this issue Dec 3, 2023 · 1 comment

Comments

@nlwillia
Copy link

nlwillia commented Dec 3, 2023

Version

29.1.1

Steps to reproduce

I'm trying to setup ts-jest for ESM testing. While working through other jest weirdness, I found that setting their "cache" option to false prevents @jest/globals from being resolved.

The zipped package minimally demonstrates this. Jest config is inline in package.json.

  "jest": {
    "cache": false,
    "extensionsToTreatAsEsm": [".ts"],
    "moduleNameMapper": {
      "^(\\.{1,2}/.*)\\.js$": "$1"
    },
    "transform": {
      "^.+\\.tsx?$": [
        "ts-jest",
        {
          "useESM": true
        }
      ]
    }
  }

ts-jest-cache.zip

Expected behavior

The test should succeed regardless of the cache setting.

Actual behavior

npm run test produces:

 FAIL  __tests__/cache.spec.ts
  ● Test suite failed to run

    __tests__/cache.spec.ts:1:20 - error TS2792: Cannot find module '@jest/globals'. Did you mean to set the 'moduleResolution' option to 'nodenext
', or to add aliases to the 'paths' option?

Debug log

None was produced.

Additional context

I realize that this is a jest option, but it doesn't have this effect in a .js test.

Environment

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
  Binaries:
    Node: 20.9.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@SReject
Copy link

SReject commented Dec 17, 2023

It randomly happens even when cache property is not manually set:

Env

Win 10
Node v18.14.0
npm v9.3.1

{
    // package.json

    "devDependencies": {
        "@jest/globals": "29.1.1",
        "jest": "29.1.1",
        "ts-jest": "29.1.1",
        "typescript": "^5.3.3"
    },
    "jest": {
        "preset": "ts-jest/presets/default-esm",
        "collectCoverage": true,
        "coverageDirectory": "coverage",
        "testEnvironment": "node",
        "testMatch": [
            "**/*.spec.ts"
        ],
        "transform": {
            "^.+\\.tsx?$": [
                "ts-jest",
                { "diagnostics": { "ignoreCodes": [ "TS151001" ] } }
            ]
        }
    }
}
// ts config
{
    "compilerOptions": {
        "module": "ESNext",
        "noImplicitAny": true,
        "sourceMap": true,
        "baseUrl": ".",
        "rootDir": ".",
        "target": "ESNext",
        "outDir": "./dist",
        "skipLibCheck": true
    },
    "files": ["src/index.ts"]
}

Edit

Did a bit more testing on my end and it seems to be an issue related to import v. require;

If I require the globals the tests consistently pass without the afore mentioned error even when jest's cache option is set to false. The draw back is that type information is lost:

// typed as any
const { describe, test, expect } = require('@jest/globals');

// ...tests ...

If I use import I get the error the first time, but subsequent runs do not:

import { describe, test, expect } from '@jest/globals';

// ...tests ...

Maybe the caching mechanism is using import() but not waiting for it to resolve?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants