kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.89k stars 448 forks source link

[Bug]: error TS2307: Cannot find module '...' or its corresponding type declarations. #4368

Closed francoismassart closed 3 weeks ago

francoismassart commented 3 weeks ago

Version

29.1.4

Steps to reproduce

  1. Clone this repo: https://github.com/francoismassart/eslint-plugin-demo-typescript/tree/feat/test
  2. Make sure you are on the feat/test branch
  3. pnpm i
  4. pnpm build runs fine
  5. pnpm test which executes jest fails with this error: tests/my-rule.spec.ts:1:28 - error TS2307: Cannot find module '@typescript-eslint/rule-tester' or its corresponding type declarations.

Expected behavior

As the code build fine, the issue seems to be related to ts-jest

Actual behavior

> jest

ts-jest[config] (WARN) message TS151001: If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.
 FAIL  tests/my-rule.spec.ts
  ● Test suite failed to run

    tests/my-rule.spec.ts:1:28 - error TS2307: Cannot find module '@typescript-eslint/rule-tester' or its corresponding type declarations.

    1 import { RuleTester } from '@typescript-eslint/rule-tester';
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.656 s
Ran all test suites.
 ELIFECYCLE  Test failed. See above for more details.

Debug log

Logs are too long to be pasted directly into issue: https://justpaste.it/e6401

Additional context

No response

Environment

System:
    OS: macOS 13.5
    CPU: (12) arm64 Apple M2 Pro
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
    pnpm: 8.15.5 - ~/Library/pnpm/pnpm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
francoismassart commented 3 weeks ago

Extract from the (huge) logs:

{
    "config": {
        "automock": false,
        "cache": true,
        "cacheDirectory": "/private/var/folders/xd/2v4dq4r13_1c3kysysjkldd00000gn/T/jest_dx",
        "clearMocks": false,
        "collectCoverageFrom": [],
        "coverageDirectory": "/Users/fma/eslint-plugin-demo-typescript/coverage",
        "coveragePathIgnorePatterns": [
            "/node_modules/"
        ],
        "cwd": "/Users/fma/eslint-plugin-demo-typescript",
        "detectLeaks": false,
        "detectOpenHandles": false,
        "errorOnDeprecated": false,
        "extensionsToTreatAsEsm": [],
        "fakeTimers": {
            "enableGlobally": false
        },
        "forceCoverageMatch": [],
        "globals": {
            "ts-jest": {}
        },
        "haste": {
            "computeSha1": false,
            "enableSymlinks": false,
            "forceNodeFilesystemAPI": true,
            "throwOnModuleCollision": false
        },
        "id": "0e1a76e07665af9d7f1061e1c4b02546",
        "injectGlobals": true,
        "moduleDirectories": [
            "node_modules"
        ],
        "moduleFileExtensions": [
            "js",
            "mjs",
            "cjs",
            "jsx",
            "ts",
            "tsx",
            "json",
            "node"
        ],
        "moduleNameMapper": [],
        "modulePathIgnorePatterns": [],
        "openHandlesTimeout": 1000,
        "prettierPath": "prettier",
        "resetMocks": false,
        "resetModules": false,
        "restoreMocks": false,
        "rootDir": "/Users/fma/eslint-plugin-demo-typescript",
        "roots": [
            "/Users/fma/eslint-plugin-demo-typescript"
        ],
        "runner": "/Users/fma/eslint-plugin-demo-typescript/node_modules/.pnpm/jest-runner@29.7.0/node_modules/jest-runner/build/index.js",
        "sandboxInjectedGlobals": [],
        "setupFiles": [],
        "setupFilesAfterEnv": [],
        "skipFilter": false,
        "slowTestThreshold": 5,
        "snapshotFormat": {
            "escapeString": false,
            "printBasicPrototype": false
        },
        "snapshotSerializers": [],
        "testEnvironment": "/Users/fma/eslint-plugin-demo-typescript/node_modules/.pnpm/jest-environment-node@29.7.0/node_modules/jest-environment-node/build/index.js",
        "testEnvironmentOptions": {},
        "testLocationInResults": false,
        "testMatch": [
            "**/tests/**/*.ts"
        ],
        "testPathIgnorePatterns": [
            "/node_modules/"
        ],
        "testRegex": [],
        "testRunner": "/Users/fma/eslint-plugin-demo-typescript/node_modules/.pnpm/jest-circus@29.7.0/node_modules/jest-circus/runner.js",
        "transform": [
            [
                "^.+\\.tsx?$",
                "/Users/fma/eslint-plugin-demo-typescript/node_modules/.pnpm/ts-jest@29.1.4_@babel+core@7.24.7_esbuild@0.21.5_jest@29.7.0_typescript@5.4.5/node_modules/ts-jest/dist/index.js",
                {}
            ]
        ],
        "transformIgnorePatterns": [
            "/node_modules/",
            "\\.pnp\\.[^\\/]+$"
        ],
        "watchPathIgnorePatterns": []
    },
    "logLevel": 20,
    "namespace": "backports",
    "package": "ts-jest",
    "version": "29.1.4"
}
francoismassart commented 3 weeks ago

The issue has been solved on my side.

In order to use, the RuleTester from @typescript-eslint/rule-tester, we must: