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

Can't seem to use with linked typescript package #424

Closed
Anupheaus opened this issue Jan 28, 2018 · 21 comments
Closed

Can't seem to use with linked typescript package #424

Anupheaus opened this issue Jan 28, 2018 · 21 comments

Comments

@Anupheaus
Copy link

  • 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"
    ]
}
@Anupheaus
Copy link
Author

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?

@GeeWee
Copy link
Collaborator

GeeWee commented Jan 28, 2018

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

@kulshekhar
Copy link
Owner

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

@Anupheaus
Copy link
Author

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.

@kulshekhar
Copy link
Owner

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

@Anupheaus
Copy link
Author

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?

@kulshekhar
Copy link
Owner

@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

@Anupheaus
Copy link
Author

If I get time tonight I will certainly do that!

@tipng
Copy link

tipng commented Mar 6, 2018

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

@GeeWee
Copy link
Collaborator

GeeWee commented Mar 6, 2018

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

@tipng
Copy link

tipng commented Mar 7, 2018

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?

@tipng
Copy link

tipng commented Mar 7, 2018

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)

@GeeWee
Copy link
Collaborator

GeeWee commented Mar 7, 2018

What exactly do you mean with linked dependency?

@tipng
Copy link

tipng commented Mar 8, 2018

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.

@tipng
Copy link

tipng commented Mar 15, 2018

Might this be related to jestjs/jest#5085?

@GeeWee
Copy link
Collaborator

GeeWee commented Mar 19, 2018

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

@tipng
Copy link

tipng commented Mar 20, 2018

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

Thanks

@acaua
Copy link

acaua commented Apr 20, 2018

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

@rosskevin
Copy link

rosskevin commented Jul 3, 2018

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.

@rosskevin
Copy link

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.

@huafu
Copy link
Collaborator

huafu commented Sep 6, 2018

@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)

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

7 participants