node.js - 尝试在 node_modules 中查找类型定义时 Jest 失败

标签 node.js typescript jestjs babeljs

我正在开发一个由 Node 驱动的后端,使用 Babel 和 Typescript,以及 Jest(使用 ts-jest)进行测试。

直到 2 天前一切都很好,我的测试不再工作了,相反我得到了 错误 TS2688:找不到 [模块名称] 的类型定义文件

我的 node_modules 文件夹中没有 .d.ts 文件的每个模块都会发生此错误。 以下是错误的前几行供您查看:

FAIL  src/entity/OfferPool.test.ts
  ● Test suite failed to run

    TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
    error TS2688: Cannot find type definition file for '.bin'.
    error TS2688: Cannot find type definition file for '.cache'.
    error TS2688: Cannot find type definition file for '@babel'.
    error TS2688: Cannot find type definition file for '@types'.
    error TS2688: Cannot find type definition file for 'abab'.
    error TS2688: Cannot find type definition file for 'abbrev'.
    error TS2688: Cannot find type definition file for 'accepts'.
    error TS2688: Cannot find type definition file for 'acorn'.
    error TS2688: Cannot find type definition file for 'acorn-globals'.
    error TS2688: Cannot find type definition file for 'acorn-walk'.

对于我拥有的每个测试文件,我也会遇到此错误。

你知道这是从哪里来的吗?几天前它运行良好...

这里有一些可以提供帮助的配置文件:

对于 typescript :

//tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "noImplicitAny": false,
    "removeComments": true,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./src",
    "lib": ["es2015"],
    "moduleResolution": "node",
    "noEmitHelpers": true,
    "importHelpers": true,
    "strict": true,
    "noImplicitReturns": true,
    "typeRoots": [
      "node_modules"
    ]
  },
  "include": [
    "./src/**/*",
    "./src/**/*.test.ts"
  ],
  "exclude": [
    "./node_modules"
  ]
}

对于巴别塔:

//.babelrc
{
  "presets": ["@babel/preset-typescript", "@babel/preset-env"],
  "plugins": ["@babel/plugin-proposal-class-properties"]
}

开 Jest :

// jest.config.js
module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
};

还有我的依赖:

  "dependencies": {
    "@babel/polyfill": "^7.0.0",
    "@types/es6-promise": "^3.3.0",
    "@types/node": "^10.12.10",
    "aws-sdk": "^2.188.0",
    "bluebird": "^3.5.1",
    "cookie-parser": "~1.4.3",
    "cors": "^2.8.4",
    "debug": "~2.6.9",
    "dotenv": "^6.1.0",
    "express": "~4.16.0",
    "express-fileupload": "^0.4.0",
    "express-winston": "^2.6.0",
    "helmet": "^3.9.0",
    "http-errors": "~1.6.2",
    "jade": "~1.11.0",
    "jwt-simple": "^0.5.1",
    "moment": "^2.20.1",
    "morgan": "~1.9.0",
    "mysql": "^2.15.0",
    "node-fetch": "^2.3.0",
    "tslib": "^1.9.3",
    "winston": "^3.1.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.1.2",
    "@babel/core": "^7.1.2",
    "@babel/node": "^7.0.0",
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
    "@babel/preset-env": "^7.1.6",
    "@babel/preset-typescript": "^7.1.0",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.9",
    "@types/node-fetch": "^2.1.4",
    "babel-eslint": "^8.2.6",
    "babel-jest": "^23.6.0",
    "jest": "^23.6.0",
    "nodemon": "^1.18.1",
    "ts-jest": "^23.10.5",
    "typescript": "^3.1.3"
  }

最佳答案

我认为您没有正确使用 typeRoots。这可能会解决您的问题,https://www.typescriptlang.org/docs/handbook/tsconfig-json.html默认是使用 node_modules/@types

关于node.js - 尝试在 node_modules 中查找类型定义时 Jest 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53446517/

相关文章:

node.js - 如果一个 npm 包附带多个 dist 文件夹,我如何知道在构建我的应用程序时使用了哪个?

C#/TypeScript 字节数组在发送到客户端时得到编码

node.js - Jest 不运行——无限期挂起

javascript - 为什么我不能在 Nodejs v10 中读取 "import * as readline from ' readline'"?

javascript - eslint 应该列在项目的依赖项中,而不是 devDependencies

javascript - Angular Directive(指令) - 自动拉伸(stretch)到底部

javascript - 开 Jest : Share Variables Between Test Files

javascript - 如何在 enzyme Jest 测试中定义 Prop

javascript - 使用 webos 将 html5 canvas 元素保存到文件

node.js - 在 Angular 5 中使用 electron-builder 构建 Electron 应用程序