ts-jest: Can't seem to use with linked typescript package

  • Issue

I have a project with a linked TypeScript package but when I run the tests they all fail with: Cannot find module 'ts-jest' from 'index.ts'

  • Expected behavior

I have followed the configuration advised to make imported TypeScript packages work and I would expect it to transpile the code within the imported package as well.

  • Link to a minimal repo that reproduces this issue

I will try and create one

Here is my package.json config:

  "jest": {
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!@personal)/.*"
    ],
    "testRegex": ".*\\.tests\\.(ts|tsx)$",
    "globalSetup": "./tests/setup.js"
  },

The imported package is @personal/common.

The tests fail when it tries to parse ...node_modules/@personal/common/index.ts so I know it is something to do with ts-jest not being used on the imported package.

my ts-config configuration is:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "jsx": "react",
        "module": "commonjs",
        "lib": [
            "es2016",
            "dom"
        ],
        "target": "es2015",
        "noImplicitAny": false,
        "sourceMap": true,
        "watch": true
    },
    "compileOnSave": true,
    "exclude": [
        "node_modules",
        "!node_modules/@personal"
    ]
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 21 (3 by maintainers)

Most upvoted comments

I had a similar problem and found a fix.

I think it occurs because Jest is looking for ts-jest on the “node_modules” folder from the second project (not the main project that have ts-jest installed). That’s why installing ts-jest on the second project works as a workaround.

I solved it by setting the “node_modules” folder explicitly on the Jest config file using the line bellow:

moduleDirectories: ['<rootDir>/node_modules'],

Thanks @acaua! This seems to be suddenly afflicting my yarn workspaces based monorepo as well.

I explicitly added this to a jest.config.js:

moduleDirectories: ['<rootDir>/node_modules', '<rootDir>/../../node_modules'],

(which I expect is the default behavior) and it fails. Change that to:

moduleDirectories: ['<rootDir>/../../node_modules'],

and all is well. I’m not sure how this corner case appears but I wouldn’t expect this workaround to be needed.

Okay, so I am having the same issue.

I installed ts-jest in the linked dependency and the error changed to:

TypeError: require(...).install is not a function

If I get time tonight I will certainly do that!

Nope, I installed it in the main package and I mean an npm link to the dependent package. Definitely installed because it is working on a test that does not depend on my @personal/common package.