Unable to run jest with typescript - "Cannot find module 'typescript'"
11,653
@tmhao kindly helped me with this issue.
Problem was that ts-jest was not installed. A bit unclear error message.
Author by
Jørgen Vik
Updated on September 13, 2022Comments
-
Jørgen Vik over 1 year
I'm trying to run jest with typescript, but I'm getting following error. The project runs fine in webpack and with ts-node. For some reason I can't get it to work with jest.
FAIL src/__tests__/classes/Utils.spec.ts ● Test suite failed to run Cannot find module 'typescript' Require stack: - /Users/myuser/repos/project/node_modules/ts-jest/dist/config/config-set.js - /Users/myuser/repos/project/node_modules/ts-jest/dist/ts-jest-transformer.js - /Users/myuser/repos/project/node_modules/ts-jest/dist/index.js - /Users/myuser/repos/project/node_modules/@jest/transform/build/ScriptTransformer.js - /Users/myuser/repos/project/node_modules/@jest/transform/build/index.js - /Users/myuser/repos/project/node_modules/jest-runtime/build/index.js - /Users/myuser/repos/project/node_modules/@jest/core/build/cli/index.js - /Users/myuser/repos/project/node_modules/@jest/core/build/jest.js - /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/build/cli/index.js - /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/bin/jest.js - /Users/myuser/repos/project/node_modules/jest/bin/jest.js - /usr/local/lib/node_modules/jest/node_modules/import-local/index.js - /usr/local/lib/node_modules/jest/bin/jest.js
jest.config.js
This is the configuration. I've tried many variations on the roots property and moduleNameMapper, but the error message is exactly the same no matter what I change in the config.
module.exports = { preset: 'ts-jest', testEnvironment: 'node', roots: ['<rootDir>/src'], transform: { '^.+\\.tsx?$': 'ts-jest', }, testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], moduleDirectories: ['node_modules', 'src'], moduleNameMapper: { '^src/(.*)$': '<rootDir>/src/$1', } };
tsconfig.json
{ "compilerOptions": { "baseUrl": "./", "paths": { "src/*": ["src/*"], "tests/*": ["__tests__/*"] }, "target": "es6", "module": "commonjs", "esModuleInterop": true, "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true, "inlineSourceMap": false, "outDir": "dist", "lib": ["es6", "dom", "esnext", "esnext.asynciterable"], "typeRoots": ["node_modules/@types", "src/typings"] }, "awesomeTypescriptLoaderOptions": { "useBabel": true, "useCache": true, "useTranspileModule": true }, "include": ["src/**/*.ts"], "exclude": ["./node_modules/*"] }
Utils.spec.js
import { Utils } from 'src/utils/classes/Utils'; test('Generates a 6 digit number'), () => { expect(Utils.randomNumberGenerator(6).toHaveLength(6)) });
Utils.ts
export class Utils { public static randomNumberGenerator(length: number): number { const baseNumber: number = Number(1 + '0'.repeat(length - 1)); const randomMultiplier: number = Math.floor(Math.random() * Number('9' + '0'.repeat(length - 1))); const randomToken: number = baseNumber + randomMultiplier; return randomToken; } }
-
tmhao2005 over 3 yearsIt looks like you haven't installed typescript in your local deps?
-
Jørgen Vik over 3 years@tmhao2005 "typescript": "^3.3.3333" can be located in packages.json. Ran an npm install typescript just in case and same problem.
-
tmhao2005 over 3 yearsWere you successful to install that kind of weird version? It’s supposed to be like 3.9.7 instead
-
Jørgen Vik over 3 years@tmhao2005 Yes, but since you pointed it out I tried to upgrade by running
npm i typescript@latest
which installed typescript 4.0.2 for me. Still same issue. -
tmhao2005 over 3 yearsCan you share your repo structure and your package.json as well?
-
Jørgen Vik over 3 years@tmhao2005 pastebin.com/TcU5FUDL output from tree -L 2 (without node modules) Package.json: pastebin.com/pQsN0scq
-
Jørgen Vik over 3 yearsLet us continue this discussion in chat.
-
tmhao2005 over 3 yearsLooks like you installed your jest stuff globally which ends up the issue so try install locally and try again
-
-
Dmitrii Rogozin about 2 yearsThank you sir :D I do agree the error message is not the best