5

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;
    }
}
8
  • 1
    It looks like you haven't installed typescript in your local deps? Commented Aug 21, 2020 at 10:01
  • @tmhao2005 "typescript": "^3.3.3333" can be located in packages.json. Ran an npm install typescript just in case and same problem. Commented Aug 21, 2020 at 10:16
  • 2
    Were you successful to install that kind of weird version? It’s supposed to be like 3.9.7 instead Commented Aug 21, 2020 at 10:19
  • @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. Commented Aug 21, 2020 at 10:24
  • 1
    Looks like you installed your jest stuff globally which ends up the issue so try install locally and try again Commented Aug 21, 2020 at 10:43

1 Answer 1

9

@tmhao kindly helped me with this issue.

Problem was that ts-jest was not installed. A bit unclear error message.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sir :D I do agree the error message is not the best
Wow, thank you! The following error clued me in node_modules/jest-cli/build/cli/index.js:161 if (error?.stack) { ^ SyntaxError: Unexpected token .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.