Jest: Jest cannot use ts-jest when installed globally

Created on 11 Jul 2018  ·  5Comments  ·  Source: facebook/jest

🐛 Bug Report

I can run tests with a local install...

npm install --save-dev jest ts-jest typescript

But a global install causes this error when jest is executed:

● Validation Error:
  Module ts-jest in the transform option was not found.

(This occurs even if ts-jest, but not jest, is installed locally in the project.)

To Reproduce

  • Run these terminal commands (including install of babel-core to avoid a "peer dependency" warning):

    mkdir temp
    cd temp
    npm init -f
    npm install --global jest ts-jest typescript babel-core
    
  • Add package.json configuration for ts-jest:

    "jest": {
    "transform": {
    "^.+\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\.|/)(test|spec))\.(jsx?|tsx?)$",
    "moduleFileExtensions": ["ts","tsx","js","jsx","json"]
    }

  • Run jest to get "Module ts-jest in the transform option was not found."

Expected behavior

If the error does not occur, the output is "No tests found"

Run npx envinfo --preset jest

npx: installed 1 in 2.921s

  System:
    OS: Windows 10
    CPU: x64 Intel(R) Core(TM) i5 CPU         750  @ 2.67GHz
  Binaries:
    npm: 6.1.0 - C:\Program Files\nodejs\npm.CMD

Most helpful comment

Can you clarify @SimenB ? "Module ts-jest in the transform option was not found." is definitely not an error that ts-jest is throwing. (I grepped our codebase to make sure) - are you sure this is not an error thrown by jest when it can't find the specified preprocessor?

All 5 comments

This is a bug with ts-jest, not us - that's a custom error they throw. If you open up an issue over there and they disagree, we can take another look on our side :)

Can you clarify @SimenB ? "Module ts-jest in the transform option was not found." is definitely not an error that ts-jest is throwing. (I grepped our codebase to make sure) - are you sure this is not an error thrown by jest when it can't find the specified preprocessor?

Updating the package.json transform location worked for me; hope this helps

"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"

Ensuring that jest was installed locally resolved this issue for me as well. Seems like another piece of evidence that a global install of jest may be part of the cause.

npm install --save-dev jest

What solved for me was the path to babel-jest in the "transform" key inside the jest.config.js file -

transform: {
    "\\.js$": "../node_modules/babel-jest" 
}

Change it to:

transform: { . 
    "\\.js$": "<rootDir>/node_modules/babel-jest" // Use <rootDir> . 
 },
Was this page helpful?
0 / 5 - 0 ratings