【问题标题】:How to run jest on a typescript/expressjs app with typeRoots enabled on tsconfig.json如何在 tsconfig.json 上启用 typeRoots 的 typescript/expressjs 应用程序上运行 jest
【发布时间】:2021-10-07 11:30:02
【问题描述】:

嗯,我有这个项目here,它包含 typescript 和 expressjs。我现在的目标是构建测试,但是当我运行 yarn test(运行 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',
};

【问题讨论】:

    标签: typescript express jestjs ts-jest


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 2019-10-12
      • 2020-07-09
      • 1970-01-01
      • 2018-12-10
      • 2017-08-16
      • 2017-12-03
      • 1970-01-01
      相关资源
      最近更新 更多