小编dan*_*ald的帖子

Nestjs单元测试createTestingModule依赖注入

我希望你能帮助我。\n我正在使用 Nx 和最新的 Angular/nestjs(日期:2 月 26 日)

\n
    ...\n    "@nestjs/common": "^7.0.0",\n    "@nestjs/config": "^0.6.3",\n    "@nestjs/core": "^7.0.0",\n    "@nestjs/platform-express": "^7.0.0",\n    "@nestjs/platform-socket.io": "^7.6.7",\n    "@nestjs/websockets": "^7.6.7",\n    "jest": "26.2.2",\n    "@nrwl/jest": "11.4.0",\n    ...\n
Run Code Online (Sandbox Code Playgroud)\n

我无法使用 NestJS 和 Jest 运行单元测试\n我想测试以下服务:

\n
@Injectable()\nexport class CoreApiService {\n  logger = new Logger('CoreApiService');\n  apiEndpoint;\n\n  constructor(private httpService: HttpService, configService: ConfigService) {\n    this.apiEndpoint = configService.get('API_SERVICE_ENDPOINT');\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我收到以下错误:

\n
TypeError: Cannot read property 'get' of undefined\n\n
Run Code Online (Sandbox Code Playgroud)\n

所以看起来 ConfigService (还有 httpService)总是未定义的。

\n

当记录 httpService 和 ConfigService 时,它​​始终是未定义的。\n即使当我尝试实例化新实例时,new CoreApiService(new HttpService(), new ConfigService())\n我什至尝试过类似new …

nomachine-nx typescript jestjs nestjs

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

jestjs ×1

nestjs ×1

nomachine-nx ×1

typescript ×1