简体   繁体   中英

jest stop working after setting "type": "module" in tsconfig.json

I am using jest for unit testing in typescript. When setting "type": "module" in the tsconfig.json, i can run my app, but my jest stops working and say "ReferenceError: require is not defined".

const { pathsToModuleNameMapper } = require("ts-jest/utils");

const { compilerOptions } = require("./tsconfig");

const paths = pathsToModuleNameMapper(compilerOptions.paths, { prefix: "<rootDir>/src" });

delete paths["^(.*)$"];

Eslint is giving me "Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser".

I worked around this by specifying a modified duplicate of the main tsconfig.

Point to the new file in jest's config:

// package.json example

{
  // [...]
  "jest": {
    "globals": {
      "ts-jest": {
        "tsConfig": "tsconfig.test.json"
      }
    }
  }
}

Credit: https://huafu.github.io/ts-jest/user/config/tsConfig

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