尝试在node_modules中查找类型定义时,玩笑失败

时间:2018-11-23 12:12:32

标签: node.js typescript jestjs babel

我正在研究由Node支持的后端,使用Babel和Typescript以及Jest(带有ts-jest)进行测试。

直到2天前,一切都很好,我的测试不再起作用,相反,我得到了 error TS2688: Cannot find type definition file for [module name]

此错误发生在我的 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'.

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

您知道这可能来自哪里吗?几天前效果很好...

以下一些配置文件可以提供帮助:

对于打字稿:

//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"
  ]
}

对于Babel:

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

开玩笑:

// 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"
  }

1 个答案:

答案 0 :(得分:1)

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