【问题标题】:Nestjs Repository test fails with error "Received promise resolved instead of rejected"Nestjs 存储库测试失败并出现错误“收到的承诺已解决而不是被拒绝”
【发布时间】:2021-08-17 04:12:47
【问题描述】:

我正在为我的后端应用程序编写单元测试,我正在努力测试数据库中未找到的项目,这是我要测试的存储库的代码:

@EntityRepository(Collectible)
export class CollectibleRepository extends Repository<Collectible> {
  async getCollectible(id: number) {
    const collectible = await this.findOne(id);
    if (!collectible) {
      throw new NotFoundException();
    }
    return collectible;
  }
}

这是测试代码,我只展示这个测试用例。

const mockCollectible = new Collectible(
  faker.lorem.sentence(),
  faker.lorem.sentences(3),
  faker.datatype.float(),
  faker.datatype.float(),
  faker.datatype.number(),
);

describe('Collectible Repository', () => {
  let collectibleRepository: CollectibleRepository;

  beforeEach(async () => {
    const module = await Test.createTestingModule({
      providers: [CollectibleRepository],
    }).compile();

    collectibleRepository = module.get<CollectibleRepository>(
      CollectibleRepository,
    );
  });

  describe('View Collectibles', () => {
       it('throws and error as the collectible is not found', async (done) => {
      collectibleRepository.findOne = jest.fn().mockResolvedValue(undefined);
      await expect(collectibleRepository.getCollectible(1)).rejects.toThrow(
        NotFoundException,
      );
      done();
    });
  });
});

这会导致以下错误输出:

Expected message: not "Not Found"

       8 |     const collectible = await this.findOne(id, { relations: ['business'] });
       9 |     if (!collectible) {
    > 10 |       throw new NotFoundException();
         |             ^
      11 |     }
      12 |     return collectible;
      13 |   }

      at CollectibleRepository.getCollectibleBusiness (src/collectible/collectible.repository.ts:10:13)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:95012) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:95012) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 FAIL  src/collectible/collectible.repository.spec.ts (8.525 s)
  ● Collectible Repository › View Collectibles › throws and error as the collectible is not found

    expect(received).rejects.not.toThrow()

    Received promise resolved instead of rejected
    Resolved to value: undefined

      39 |     it('throws and error as the collectible is not found', async (done) => {
      40 |       collectibleRepository.findOne = jest.fn().mockResolvedValue(undefined);
    > 41 |       await expect(collectibleRepository.getCollectible(1)).rejects.not.toThrow(
         |             ^
      42 |         NotFoundException,
      43 |       );
      44 |       done();

      at expect (../node_modules/expect/build/index.js:134:15)
      at Object.<anonymous> (collectible/collectible.repository.spec.ts:41:13)

我尝试使用this repository(在另一个 SO 线程中提到)和一组示例来测试 Nest.js 应用程序,但似乎没有直接测试存储库。

更新:我更新了我的代码,因为我的代码中缺少 await(如 Micael Levi 所述),我也调用了错误的函数(哈哈)。我收到以下警告

(node:98378) UnhandledPromiseRejectionWarning: Error: expect(received).rejects.not.toThrow(expected)

虽然我可能会忽略它,除非它影响我的 CI 管道(我需要配置 lmao)

更新 2:警告是由另一个测试引起的(我现在可以休息了)。

【问题讨论】:

    标签: node.js typescript unit-testing jestjs nestjs


    【解决方案1】:

    你错过了const collectible = this.findOne(id);中的await

    所以

    const collectible = await this.findOne(id);

    【讨论】:

    • 看起来这是一个原因,我还注意到我的一个错字。尽管我收到(node:98378) UnhandledPromiseRejectionWarning: Error: expect(received).rejects.not.toThrow(expected) 的警告,但测试确实通过了
    猜你喜欢
    • 1970-01-01
    • 2019-04-23
    • 2021-12-20
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多