Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

enter image description here

Thank you!

like image 601
Ricky Avatar asked Oct 25 '25 00:10

Ricky


1 Answers

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`);
});
like image 90
Ricky Avatar answered Oct 27 '25 00:10

Ricky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!