开玩笑地编写单元测试时预期会出现特定错误

时间:2019-05-17 21:35:05

标签: unit-testing exception jestjs nestjs

我使用nestjs(6.5.0)和jest(24.8)并具有引发错误的方法:

  public async doSomething(): Promise<{ data: string, error?: string }> {
    throw new BadRequestException({ data: '', error: 'foo' });
  }

我如何编写一个单元测试来检查我们是否获得了带有预期数据的预期异常?显而易见的解决方案是:

it('test', async () => {
  expect(await userController.doSomething())
    .rejects.toThrowError(new BadRequestException({ data: '', error: 'foo'});
});

但是不起作用,因为new BadRequestException()用不同的调用堆栈创建了一个对象。我该如何测试?

2 个答案:

答案 0 :(得分:0)

jest documentation中的示例相比,您在这里可能有两个问题。

  • await应该在expect参数之外
  • rejects意味着引发了错误,因此您需要测试是否相等

类似的东西:

it('test', async () => {
  await expect(userController.doSomething())
    .rejects.toEqual(new BadRequestException({ data: '', error: 'foo'});
});

答案 1 :(得分:0)

回答我自己的问题:

使用自定义匹配器(见下文),测试可以写为:

it('test', async () => {
  await expect(userController.doSomething()).rejects.toContainException(
    new BadRequestException({ data: '', error: 'foo' }),
  );
});

自定义匹配项:

import { HttpException } from '@nestjs/common';

// ensure this is parsed as a module.
export {};

// https://stackoverflow.com/questions/43667085/extending-third-party-module-that-is-globally-exposed

declare global {
  namespace jest {
    interface Matchers<R> {
      toContainException: (expected: R | any) => {};
    }
  }
}

// this will extend the expect with a custom matcher
expect.extend({
  toContainException<T extends HttpException>(received: T, expected: T) {
    const success =
      this.equals(received.message, expected.message) &&
      this.equals(received.getStatus(), expected.getStatus());

    const not = success ? ' not' : '';
    return {
      message: () =>
        `expected Exception ${received.name}${not} to be ${expected.name}` +
        '\n\n' +
        `Expected: ${this.utils.printExpected(expected.message)}, ` +
        `status: ${this.utils.printExpected(expected.getStatus())} \n` +
        `Received: ${this.utils.printReceived(received.message)}, ` +
        `status: ${this.utils.printReceived(received.getStatus())}`,
      pass: success,
    };
  },
});