简体   繁体   English

在 tsconfig.json 中设置 "type": "module" 后开玩笑停止工作

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

I am using jest for unit testing in typescript.我在 typescript 中使用 jest 进行单元测试。 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".在 tsconfig.json 中设置“type”:“module”时,我可以运行我的应用程序,但我的玩笑停止工作并说“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". Eslint 给我“解析错误:已为 @typescript-eslint/parser 设置了“parserOptions.project”。

I worked around this by specifying a modified duplicate of the main tsconfig.我通过指定主 tsconfig 的修改副本来解决这个问题。

Point to the new file in jest's config:指向 jest 配置中的新文件:

// package.json example

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tsconfig.json 中模块类型的区别 - Difference between module type in tsconfig.json 当我将 tsconfig.json 中的“模块”更改为“ESNext”并将 package.json 中的“类型”更改为“模块”时,出现错误 - When I change "module" in tsconfig.json to 'ESNext' and "type" in package.json to 'module', I get an error tsconfig.json 与 node.js 模块一起使用的最佳设置是什么? - What is the best setting for tsconfig.json working with node.js modules? 找不到模块,因为 tsconfig.json 中提到了路径 - cannot find module since the path is mentioned in the tsconfig.json 导入不适用于 tsconfig.json 中的“模块”:“ESNEXT” - import doesn't work with “module”: “ESNEXT” in tsconfig.json “找不到模块 'react'”和“找不到 tsconfig.json” - “Cannot find module 'react'” and “tsconfig.json not found” 来自 tsconfig.json 的路径在 tsc 之后不起作用 - paths from tsconfig.json doesn't work after tsc 打字稿错误在 tsconfig.json 中将模块更改为“umd”或“amd”时找不到模块“typescript-Collections” - Typescript error Cannot find module 'typescript-Collections' when changing the module to “umd” or “amd” in tsconfig.json “语法错误:不能在模块外使用导入语句”但 tsconfig.json `module` 已经 `commonjs` - "SyntaxError: Cannot use import statement outside a module" but tsconfig.json `module` already `commonjs` tsconfig.json 的“排除”属性不被尊重 - "exclude" property of tsconfig.json is not being respected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM