Jest's matcher error: received value must be a function

Jest's matcher error: received value must be a function

我在 fontend 方面工作,测试我的库时,我收到了我预期的错误,但我无法做出断言。我有下一个代码:

it('If the username is not entered, I get an error', () => {
    try {
      new SDK('test', {
        password: 'aaaabbbbcccc',
        key: 'ddddeeefff',
      });
    } catch (error) {
      expect(error).toThrow(
        `Error: The 'username' property has not been entered`
      );
    }
  });

如您所见,为了对我的图书馆进行身份验证,我需要:用户名、密码和密钥。这是我得到的错误:

谢谢!

我发现了我的问题。 "expect" 有必要捕获异常然后使用适当的匹配器(没有 try/catch 块)

it('If the username is not entered, I get an error', () => {
  expect(() => {
    new SDK('test', {
      password: 'aaaabbbbcccc',
      key: 'ddddeeefff',
    });
  }).toThrowError(`The 'username' property has not been entered`);
});