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

Created on 28 Jan 2018  路  21Comments  路  Source: kulshekhar/ts-jest

  • 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"
    ]
}
closed due to inactivity - can be reopened later

Most helpful comment

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'],

All 21 comments

I've solved this problem temporarily by importing ts-jest in the linked package as a devDependency, but this doesn't seem like it is the right solution to me, any advice please?

I don't think I understand what you mean with a linked package. Can you provide a minimal repo?

@Anupheaus it sounds like you were trying to use ts-jest without installing it. Is that correct?

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.

that's weird. A minimal repo would be very welcome because it's hard to 'imagine' what could be causing this

Sorry for the inactivity!

@kulshekhar I wonder if you meant "globally installed"? I didn't globally install ts-jest, I assumed that it would work automatically in linked packages (within node_modules) but that could be a false assumption and it needs to be installed globally in order to do that? Is that right?

@Anupheaus I didn't mean 'globally installed'. My first thought was that maybe ts-jest wasn't installed in your project (but as you pointed out, that was incorrect)

If you can create a minimal repo that replicates this issue, I'll be happy to reopen this and take a look at it

If I get time tonight I will certainly do that!

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

Do you have jest installed in the project? Please provide a minimal reproducible repo.

Jest, and ts-jest are installed in both the main project, and the npm-linked dependency.

However I don't see why whatever is installed in the linked dependency should matter. We're not testing the dependency, we're testing components that are just importing and using something from it.

What would a minimal reproducible repo with a linked dependency look like?

In my main project the stack trace indicates the error origin being ts-jest in the linked dependency.

Should it be even looking for ts-jest in the linked dependency?

TypeError: require(...).install is not a function
at Object.<anonymous> (../my-dependency/node_modules/ts-jest/index.js:1:122)

What exactly do you mean with linked dependency?

Okay, I haven't explained myself thoroughly, sorry.

Let me paint you a picture, maybe this will help:

Let's say I have a React project called "MainProj" and I have a separate project that handles all of the communication to my back-end called "DataClient" which is hosted on an internal repository -- both written in typescript -- and the latter is a dependency of the former.

I need to work on a new component in MainProj, which will need to get new data from the back-end and as such, I have to create a new service in my DataClient. So between the two I use npm link to link the project DataClient into my MainProj. However whenever when I develop the feature on both projects and everything seems working, I start writing unit tests for my component, none of them work, and the aforementioned errors are shown.

Now I may have tests using Jest and ts-jest inside my DataClient dependency or I may not, but that's not supposed to matter here. The important thing to note is that both Jest and ts-jest are installed in my MainProj, where only the tests that are importing the DataClient dependency are failing with said errors.

This only happens when I have used npm link to link my DataClient dependency with my MainProj and I run the MainProj tests.

If anything isn't clear please don't hesitate to ask, I'll be more than happy to explain as I've been trying to understand what's happening for a while now.

Yeah I don't think it's on our end - we just transpile the code jest gives us.

Yeah, I figured as much... anyway I'll keep an eye on the next Jest release and see if it helps.

Thanks

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.

So this came around and bit me again. My ^^^workaround started causing node_module resolution failures. I removed the moduleDirectories override and it is back to working....for now.

@rosskevin your issue if it is an issue is a jest issue. Cannot find module 'ts-jest' from 'index.ts' is within Jest, when it tries to load a transformer (ie processor) defined as ts-jest in transform's option of jest.
One thing you could try is in jest config: "transform": { "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest" } (change according to what is your rootDir in jest of course, but it should be ok as it looks like you do not have it defined)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

artola picture artola  路  3Comments

stangerjm picture stangerjm  路  4Comments

Slessi picture Slessi  路  3Comments

TKJohn picture TKJohn  路  4Comments

mikeyakymenko picture mikeyakymenko  路  3Comments