当我使用 Bazel Test 时,Nest 无法解析 RootTestModule 上下文中的依赖关系

Posted

技术标签:

【中文标题】当我使用 Bazel Test 时,Nest 无法解析 RootTestModule 上下文中的依赖关系【英文标题】:Nest can't resolve dependencies in the RootTestModule context when I use Bazel Test 【发布时间】:2020-11-06 13:26:19 【问题描述】:

我需要在挡板中运行我的测试。我该如何解决这个神秘的问题?

我有一个包含多个应用程序和库的 nestjs 项目。当我运行测试yarn jest --config ./jest.config.json libs/lib1 时,它运行良好。但是,当我使用 bezel bazel test //libs/lib1/... 运行时,它会给我一个错误“Nest 无法解析依赖项......请确保索引处的参数依赖项......在 RootTestModule 上下文中可用。”。

回购

https://github.com/smhmayboudi/bazel_jest_nestjs

我发现 jest.config.json 的映射顺序很重要。 这个效果很好(显示测试+覆盖),但是依赖性问题

  "moduleNameMapper": 
    "@melo/lib1": "<rootDir>/libs/lib1/src",
    "@melo/lib1/(.*)": "<rootDir>/libs/lib1/src/$1",
  ,

这个有效(只显示没有实际测试结果和覆盖范围的传递消息!?)

  "moduleNameMapper": 
    "@melo/lib1/(.*)": "<rootDir>/libs/lib1/src/$1",
    "@melo/lib1": "<rootDir>/libs/lib1/src",
  ,

笑话配置


  "coverageReporters": ["lcov", "text-summary"],
  "moduleNameMapper": 
    "@melo/libs1": "<rootDir>/libs/libs1/src",
    "@melo/libs1/(.*)": "<rootDir>/libs/libs1/src/$1",
  ,
  "modulePathIgnorePatterns": ["/bazel-out/", "/node_modules/"],
  "preset": "ts-jest",
  "testEnvironment": "node"

Bazel 配置

ts_library(
    name = "lib1_test_ts_library",
    srcs = glob(["*spec.ts"]),
    runtime = "nodejs",
    deps = [
        ":lib1_ts_library",
        "@npm//@nestjs/common",
        "@npm//@nestjs/testing",
        "@npm//@types/jest",
        "@npm//rxjs",
        "@npm//ts-jest",
    ],
)

jest_test(
    name = "lib1_jest_test",
    srcs = glob(["*spec.ts"]),
    jest_config = "//:jest.config.json",
    deps = [
        ":lib1_test_ts_library",
    ],
    coverage = True,
)

错误日志

INFO: Invocation ID: 84f45d55-c6e4-4c2a-b05d-367d0f84baf7
INFO: Analyzed target //libs/lib1/src:lib1_jest_test (633 packages loaded, 19569 targets configured).
INFO: Found 1 test target...
WARNING: failed to create one or more convenience symlinks for prefix 'dist/':
  cannot create symbolic link bazel-out -> /Users/WHOAMI/Developer/MY_PROJECT/bazel-out/execroot/melo/bazel-out:  /Users/WHOAMI/Developer/MY_PROJECT/bazel-out (File exists)
FAIL: //libs/lib1/src:lib1_jest_test (see /Users/WHOAMI/Developer/MY_PROJECT/bazel-out/execroot/melo/bazel-out/darwin-fastbuild/testlogs/libs/lib1/src/lib1_jest_test/test.log)
INFO: From Testing //libs/lib1/src:lib1_jest_test:
==================== Test output for //libs/lib1/src:lib1_jest_test:
PASS libs/lib1/src/lib1.util.spec.ts (23.866 s)
PASS libs/lib1/src/lib1.interceptor.spec.ts (23.977 s)
FAIL libs/lib1/src/lib1.service.spec.ts (24.717 s)
  ● ApmService › should be defined

    Nest can't resolve dependencies of the ApmService (?). Please make sure that the argument dependency at index [0] is available in the RootTestModule context.

    Potential solutions:
    - If dependency is a provider, is it part of the current RootTestModule?
    - If dependency is exported from a separate @Module, is that module imported within RootTestModule?
      @Module(
        imports: [ /* the Module containing dependency */ ]
      )

      at Injector.resolveSingleParam (../../../../../../../../node_modules/@nestjs/core/injector/injector.js:134:19)
      at resolveParam (../../../../../../../../node_modules/@nestjs/core/injector/injector.js:102:49)
          at Array.map (<anonymous>)
      at Injector.resolveConstructorParams (../../../../../../../../node_modules/@nestjs/core/injector/injector.js:117:58)
      at Injector.loadInstance (../../../../../../../../node_modules/@nestjs/core/injector/injector.js:81:20)
      at Injector.loadProvider (../../../../../../../../node_modules/@nestjs/core/injector/injector.js:38:20)
      at ../../../../../../../../node_modules/@nestjs/core/injector/instance-loader.js:43:62
          at Array.map (<anonymous>)
      at InstanceLoader.createInstancesOfProviders (../../../../../../../../node_modules/@nestjs/core/injector/instance-loader.js:43:36)
      at ../../../../../../../../node_modules/@nestjs/core/injector/instance-loader.js:28:24

  ● ApmService › should be defined

    expect(received).toBeDefined()

    Received: undefined

      56 | 
      57 |   it("should be defined", () => 
    > 58 |     expect(service).toBeDefined();
         |                     ^
      59 |   );
      60 | 
      61 |   it("start should be called", () => 

      at Object.<anonymous> (libs/lib1/src/lib1.service.spec.ts:58:21)

...

Test Suites: 2 failed, 2 passed, 4 total
Tests:       27 failed, 3 todo, 6 passed, 36 total
Snapshots:   0 total
Time:        26.102 s
Ran all test suites within paths "libs/lib1/src/lib1.decorator.spec.ts", "libs/lib1/src/lib1.interceptor.spec.ts", "libs/lib1/src/lib1.service.spec.ts", "libs/lib1/src/lib1.util.spec.ts".
================================================================================
Target //libs/lib1/src:lib1_jest_test up-to-date:
  dist/bin/libs/lib1/src/lib1_jest_test.sh
  dist/bin/libs/lib1/src/lib1_jest_test_loader.js
  dist/bin/libs/lib1/src/lib1_jest_test_require_patch.js
INFO: Elapsed time: 83.878s, Critical Path: 59.53s
INFO: 4 processes: 4 local.
INFO: Build completed, 1 test FAILED, 12 total actions
//libs/lib1/src:lib1_jest_test                                             FAILED in 28.2s
  /Users/WHOAMI/Developer/MY_PROJECT/bazel-out/execroot/melo/bazel-out/darwin-fastbuild/testlogs/libs/lib1/src/lib1_jest_test/test.log

INFO: Build completed, 1 test FAILED, 12 total actions

【问题讨论】:

【参考方案1】:

将提供程序添加到您的 RootTestModule。 Nest 不会自动在您的测试中包含服务,具体取决于您是否使用 cli 与直接创建文件/文件夹。

const module: TestingModule = await Test.createTestingModule(
      providers: [/** Services goes here **/],
      controllers: [CustomersController],
).compile();

不工作 vs 在下面工作

import  Test, TestingModule  from '@nestjs/testing';
import  CustomersController  from './customers.controller';

describe('CustomersController', () => 
  let controller: CustomersController;

  beforeEach(async () => 
    const module: TestingModule = await Test.createTestingModule(
      controllers: [CustomersController],
    ).compile();

    controller = module.get<CustomersController>(CustomersController);
  );

  it('should be defined', () => 
    expect(controller).toBeDefined();
  );

  it('shoud return customer', async () => 
    const tcase = await controller.d();
    expect(tcase).toHaveProperty('firstName');
  )
);

工作(我当然有不同文件名的确切错误消息)

import  Test, TestingModule  from '@nestjs/testing';
import  CustomersController  from './customers.controller';
import  CustomersService  from './customers.service';

describe('CustomersController', () => 
  let controller: CustomersController;

  beforeEach(async () => 
    const module: TestingModule = await Test.createTestingModule(
      providers: [CustomersService],
      controllers: [CustomersController],
    ).compile();

    controller = module.get<CustomersController>(CustomersController);
  );

  it('should be defined', () => 
    expect(controller).toBeDefined();
  );

  it('shoud return customer', async () => 
    const tcase = await controller.d();
    expect(tcase).toHaveProperty('firstName');
  )
);

【讨论】:

遇到了同样的错误,但即使这样也无法正常工作。 这里一样,即使在提供者的服务中仍然遇到同样的问题。

以上是关于当我使用 Bazel Test 时,Nest 无法解析 RootTestModule 上下文中的依赖关系的主要内容,如果未能解决你的问题,请参考以下文章

无法连接到数据库 - NEST.JS 和 TypeORM

Nest 无法解析 UserModel 的依赖项(?)

当涉及来自不同兄弟的不同服务时,Nest 无法解决服务依赖关系

让 Bazel 使用 python 3

tools/test/test-setup.sh test-setup.sh 在 bazel 中做了啥

在 bazel 的 python 测试中使用二进制文件