Jest test should assert one exception but received value must be a function

I'm new at js development so I'm trying to make some TDD approach with then to train my js logic, but when I have that code my expect(function)toThrow(error) returns this error

the received value must be a function

Follow my code:

  const bin2Dec = (bin) => {  

    if(!typeof bin === 'string'){
        throw new Error('Not parsable');
    } 

    return true;   
  }

  module.exports = bin2Dec;

below my test suite:

const bin2dec = require('../index')

it("Should check if bin is string", () => {
    expect(bin2dec("100")).toBe(true);
})

it("Should expect error from bin", () => {
    expect(bin2dec(100)).toThrow('Not Parsable');
})

Upvotes: 1

Views: 563

Answers (1)

skovy
skovy

Reputation: 5650

Try updating your assertion to wrap the call to bin2dec in a function as shown in the documentation: https://jestjs.io/docs/en/expect#tothrowerror

For example:

expect(() => bin2dec(100)).toThrow('Not Parsable');

Currently, the documentation states:

Note: You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.

Upvotes: 4

Related Questions