最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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:

{
    "pilerOptions": {
        "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)$"
  }
}

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:

{
    "pilerOptions": {
        "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)$"
  }
}
Share Improve this question asked Jun 23, 2018 at 16:45 gomishagomisha 2,9204 gold badges28 silver badges33 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

First thing I noticed is that you don't have typescript in your package.json. This means you have globally installed typescript piler, run it from mand 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./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"
}
发布评论

评论列表(0)

  1. 暂无评论