Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of TypeScript ts-jest pre-processor?

I'm fairly new to TypeScript (and JavaScript) so this might be obvious, but I don't understand the point of having pre-processing modules like ts-jest for running Jest tests against TypeScript code. I'm developing a TypeScript project on Node and I'm testing everything with Jest and it's working great so far.

I'm transpiling my TypeScript code (and tests) to JavaScript (ES5) and then I'm running Jest against that transpiled ES5 JavaScript with Jest - everything is working fine without the need for ts-jest.

So what's the point of using ts-jest? In other words - under what circumstances would I use it?

I have seen advice like here to use ts-jest to "preprocess typescript files" but I don't understand that. I thought the whole point of TypeScript is that it handles all that for you, so why do you need to do that separately with ts-jest? And besides, I'm doing just fine without it.

Maybe I'm not getting:

  1. the distinction between transpiling and pre-processing
  2. the meaning of Jest's transform property

Here's my tsconfig.json:

{
    "compilerOptions": {
        "strictNullChecks": true,
        "noImplicitAny": true,
        "moduleResolution": "node",
        "target": "es5",
        "lib": [
            "es6"
        ]
    },
    "include": [
        "src/**/*",
        "test/**/*",
    ],
}

And here's a snippet of my package.json:

{
  "scripts": {
    "test": "jest",
    "dev": "nodemon ./src/app",
    "start": "node ./src/app"
  },

  "devDependencies": {
    "@types/express": "^4.16.0",
    "@types/jest": "^23.1.1",
    "jest": "^23.1.0",
    "nodemon": "^1.17.5"
  },

  "jest": {
    "transform": {},
    "testRegex": "/test/.*\\.(ts|tsx|js)$"
  }
}
like image 775
gomisha Avatar asked Jun 23 '18 16:06

gomisha


People also ask

What is TypeScript Jest?

Jest is a simple, lightweight testing framework that provides a variety of testing capabilities to JavaScript and TypeScript projects. It provides functionality like assertions, mocking, spies, running tests in parallel, prioritizing failed tests, and coverage analysis.

Do you need TS-Jest?

If you want to run typechecks while you test, you should use ts-jest . You will need to configure the transformer, as Jest by default applies Babel to . ts (and . tsx ) files.

Does Jest use TS node?

When you run jest with a jest. config. ts file it will use ts-node to compile that file, then it will pass it to ts-jest which will compile your tests, then it will pass those .


1 Answers

First thing I noticed is that you don't have typescript in your package.json. This means you have globally installed typescript compiler, run it from command line, and have .js files generated side by side with .ts files. And when you run node, it runs the .js files.

While this approach may seem convenient, it relies on a globally installed tsc, and it relies on a person manually running tsc. This makes it difficult to run the project for a person not familiar with it (even if it's you an a couple of months). To fix this, you could install typescript locally (adding it to package.json), and run it automatically (npm run start).

Now, about your questions: 1. the distinction between transpiling and pre-processing

it is actually the same thing

  1. the meaning of Jest's transform property

    this simplifies test running, you don't have to have typescript globally installed, and manually run. Ts-jest transforms it for you.

You might also want to take a look at https://github.com/TypeStrong/ts-node, which is like node, but you don't need to transpile typescript yourself.

And nodemon supports typescript like this (nodemon.js):

{
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["src/**/*.test.ts"],
  "exec": "ts-node ./src/index.ts"
}
like image 109
Herman Starikov Avatar answered Oct 04 '22 04:10

Herman Starikov