最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest's matcher error: received value must be a function - Stack Overflow

programmeradmin2浏览0评论

I'm working in the fontend side, and testing my library, I receive an error that I expect but I cannot make an assertion. I have the next code:

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`
      );
    }
  });

As you can see, for the authentication of my library, I need: username, password and key. This is the error that I get:

Thank you!

I'm working in the fontend side, and testing my library, I receive an error that I expect but I cannot make an assertion. I have the next code:

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`
      );
    }
  });

As you can see, for the authentication of my library, I need: username, password and key. This is the error that I get:

Thank you!

Share Improve this question edited May 22, 2020 at 8:49 jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked May 22, 2020 at 2:17 LuisLuis 2,1437 gold badges33 silver badges53 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

I found my problem. It was necessary for the "expect" to catch the exception and then use the appropriate matcher (without try/catch block)

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`);
});
发布评论

评论列表(0)

  1. 暂无评论