简体   繁体   中英

Using TypeScript with Jest not catching syntax errors

When compiling typescript back to js, you can specify the option --alwaysStrict which:

Parse in strict mode and emit "use strict" for each source file

So, if you are missing a compulsory parameter in a function, it gets caught by this strict mode.

We are using ts-jest so we can use Jest to test our Typescript code. The problem we face is that we have not found a way to mimic the strict parsing behaviour.

This is really annoying because all the test seem to pass, but from time to time there is a syntactic mistake that slips through the cracks and we only notice when we are creating the production build (where we set the --alwaysStrict )

In our jest.config.js we have:

module.exports = {
  roots: ["<rootDir>/src"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
  "testEnvironment": "node"
};```

ts-jest uses the tsconfig.json from your project by default.

(Note: if you want a separate config file for your tests you can configure ts-jest to use a different config file )

You can set alwaysStrict in the compilerOptions section ofthe config file and ts-jest use that flag when compiling your TypeScript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM