【问题标题】:issue with path resolved on complex app nestjs based基于复杂应用程序nestjs的路径问题已解决
【发布时间】:2020-05-27 00:38:45
【问题描述】:

我为 Nestjs 和微服务构建了一个样板(仍在进行中)

现在我遇到了一个问题,我尝试运行应用程序或运行测试,它给了我这个

位于github link的代码 我在 tsconfig.json 上添加路径的问题

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist/apps",
    "baseUrl": "./",
    "incremental": true,
    "paths": {
      "@frontapi/*": ["./apps/front-api/src/*"],
      "@config/*": ["./apps/config-server/src/*"],
      "@entities/*": ["./apps/entities-server/src/*"],
      "@devops/*": ["./apps/devops-mcu/src/*"],
      "@tasks/*": ["./apps/task-que-handler/src/*"],
      "@logicBus/*": ["./apps/logic-bus/src/*"],
      "@common/*": ["./apps/common/*"]
    }
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

然后我运行 json 包进行测试的两个命令: "start:frontapi": "npm run start:frontapi:env && tsc-watch -p tsconfig.build.json --onSuccess \"node -r tsconfig-paths/register -r ts-node/register --inspect-brk =5858 dist/front-api/src/main.js\"",

和 "test:e2e": "npm run clean&&tsc -p tsconfig.build.json && jest --config ./apps/front-api/test/jest-e2e.json",

两者都告诉我它不在@common/config/configuration 我不知道我在这里缺少什么并帮助解决这个问题

编辑他的内容有一个名为 jest-e2e.json 的文件

{
  "moduleFileExtensions": [
    "js",
    "json",
    "ts"
  ],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "moduleNameMapper": {
    "^@frontapi/(.*)$": "./apps/front-api/src/$1",
    "^@config/(.*)$": "./apps/config-server/src/$1",
    "^@entities/(.*)$": "./apps/entities-server/src/$1",
    "^@devops/(.*)$": "./apps/devops-mcu/src/$1",
    "^@tasks/(.*)$": "./apps/task-que-handler/src/$1",
    "^@logicBus/(.*)$": "./apps/logic-bus/src/$1",
    "^@common/(.*)$": "./apps/common/$1"
  }
}

如果我在这里遗漏了什么,或者您遗漏了什么,请告诉我,我会尽力提供

【问题讨论】:

  • 您的路径是否已映射到您的 jest.e2e.json 配置中?
  • @JayMcDoniel yap 只是编辑帖子显示它的内容

标签: node.js typescript express jestjs nestjs


【解决方案1】:

我也遇到了同样的问题。

查看您的项目结构后,我有一些建议/意见。我将专注于应用程序/front-api。

测试目录中有 jest.e2e.json 文件

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  ...,
  "moduleNameMapper": {
    "^@frontapi/(.*)$": "./apps/front-api/src/$1",
    "^@config/(.*)$": "./apps/config-server/src/$1",
    "^@entities/(.*)$": "./apps/entities-server/src/$1",
    "^@devops/(.*)$": "./apps/devops-mcu/src/$1",
    "^@tasks/(.*)$": "./apps/task-que-handler/src/$1",
    "^@logicBus/(.*)$": "./apps/logic-bus/src/$1",
    "^@common/(.*)$": "./apps/common/$1"
  }
}

我想指出,rootDir: '.' 引用了文件所在的目录,因此在这种情况下,rootDir 的值将是 /path/to/your/projectShowCase/backend/apps/front-api/test

重要的是你可以在moduleNameMapper 中使用相对路径。例如:

  "moduleNameMapper": {
    "^@frontapi/(.*)$": "<rootDir>/../src/$1",
    "^@config/(.*)$": "<rootDir>/../../config-server/src/$1",
    ...,
    "^@common/(.*)$": "<rootDir>/../../common/$1"
  }

您只需要将每个依赖项映射到其特定路径

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 2020-11-15
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 2019-10-24
    相关资源
    最近更新 更多