我正在使用一些代码,其中我需要测试由函数抛出的异常的类型(它是TypeError, ReferenceError等?)

我目前的测试框架是AVA,我可以测试它作为第二个参数t.throws方法,就像这里:

it('should throw Error with message \'UNKNOWN ERROR\' when no params were passed', (t) => {
  const error = t.throws(() => {
    throwError();
  }, TypeError);

  t.is(error.message, 'UNKNOWN ERROR');
});

我开始用Jest重写我的测试,但不知道如何轻松地做到这一点。这可能吗?


当前回答

您必须将期望的函数代码包装在另一个箭头函数中,否则错误将不会被捕获,断言将失败。

要测试的函数:

const testThrowingError = () => {
    throw new Error();
  };

测试:

describe("error function should Throw Error", () => {
  expect(() =>testThrowingError()).toThrowError();
});

资源: https://jestjs.io/docs/expect#tothrowerror

其他回答

您必须将期望的函数代码包装在另一个箭头函数中,否则错误将不会被捕获,断言将失败。

要测试的函数:

const testThrowingError = () => {
    throw new Error();
  };

测试:

describe("error function should Throw Error", () => {
  expect(() =>testThrowingError()).toThrowError();
});

资源: https://jestjs.io/docs/expect#tothrowerror

检查toThrow方法。

您必须将代码包装在一个额外的回调函数中!

您应该同时检查错误消息及其类型。

例如:

expect(
  () => { // additional function wrap
    yourCodeToTest();
  }
).toThrow(
  new RangeError('duplicate prevArray value: A')
);

由于额外的回调换行,代码不会立即运行,因此jest将能够捕获它。

您应该始终检查错误消息,以确保您正在检查正确的抛出情况,而不会得到代码可能抛出的另一个错误。

检查错误类型也很好,因此客户端代码可以依赖它。

除了Peter Danis的帖子,我只想强调他的解决方案中涉及“将一个函数[传递]到expect(函数)”的部分。toThrow(空白或错误类型)”。

在Jest中,当您测试应该抛出错误的情况时,在测试函数的expect()包装中,您需要提供一个额外的箭头函数包装层以使其工作。即。

错误的(但大多数人的逻辑方法):

expect(functionUnderTesting();).toThrow(ErrorTypeOrErrorMessage);

正确的:

expect(() => { functionUnderTesting(); }).toThrow(ErrorTypeOrErrorMessage);

这很奇怪,但它应该能使测试成功运行。

开玩笑地说,你必须将一个函数传递给expect(函数)。toThrow(<空白或错误类型>)。

例子:

test("Test description", () => {
  const t = () => {
    throw new TypeError();
  };
  expect(t).toThrow(TypeError);
});

或者如果你还想检查错误信息:

test("Test description", () => {
  const t = () => {
    throw new TypeError("UNKNOWN ERROR");
  };
  expect(t).toThrow(TypeError);
  expect(t).toThrow("UNKNOWN ERROR");
});

如果需要测试现有函数是否抛出一组参数,则必须将其包装在expect()中的匿名函数中。

例子:

test("Test description", () => {
  expect(() => {http.get(yourUrl, yourCallbackFn)}).toThrow(TypeError);
});

我设法把一些答案结合起来,最后得到了这样的答案:

it('should throw', async () => {
    await expect(service.methodName('some@email.com', 'unknown')).rejects.toThrow(
      HttpException,
    );
  });