10

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

我有一个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"
}

巴泽尔配置

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
4

1 回答 1

5

将提供程序添加到您的 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');
  })
});
于 2020-10-27T01:44:36.847 回答