如何在 tsconfig.json 上启用 typeRoots 的 typescript/expressjs 应用程序上 运行 开玩笑

How to run jest on a typescript/expressjs app with typeRoots enabled on tsconfig.json

嗯,我有这个项目 here,它包含 typescript 和 expressjs。我现在的目标是构建测试,但是当我 运行 yarn test(运行s jest 没有任何标志时),我得到这个错误:

PS C:\Users\joaov\Documents\gittin> yarn test
yarn run v1.22.10
$ jest
Error: Jest: Failed to parse the TypeScript config file C:\Users\joaov\Documents\gittin\jest.config.ts
  TSError: ⨯ Unable to compile TypeScript:
jest.config.ts:1:1 - error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

1 module.exports = {
  ~~~~~~

    at readConfigFileAndSetRootDir (C:\Users\joaov\Documents\gittin\node_modules\jest-config\build\readConfigFileAndSetRootDir.js:118:13)
    at readConfig (C:\Users\joaov\Documents\gittin\node_modules\jest-config\build\index.js:227:18)
    at readConfigs (C:\Users\joaov\Documents\gittin\node_modules\jest-config\build\index.js:412:26)
    at runCLI (C:\Users\joaov\Documents\gittin\node_modules\@jest\core\build\cli\index.js:220:59)
    at Object.run (C:\Users\joaov\Documents\gittin\node_modules\jest-cli\build\cli\index.js:163:37)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

有人告诉我这是因为在我的 tsconfig.json 中启用了 属性 typeRoots。当我评论那一行时,测试确实有效,但我想知道如何让 typeRoots 与 jest 一起工作,因为我需要那个本地类型。

为了更快,这些分别是 tsconfig.jsonjest.config.ts

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "dist",
    "rootDir": "src",
    "removeComments": true,
    "strict": true,
    "strictPropertyInitialization": false,
    "typeRoots": ["./src/@types"],
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": [
    "src/**/*.spec.ts",
    "src/**/*.test.ts",
    "jest.config.ts",
    "ormconfig.js",
    "node_modules"
  ]
}
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

发现我只需要将 ./node_modules/@types 添加到 tsconfig 文件中的 typeRoots 选项。