0

How can I test if a function arg it's a number? I got a simple function of leap year, so I'd like to test if the user inputs a number (not string nor boolean) in "year" argument.

const isLeapYear = (year) => ((year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0));

I tried to use mock function but didn't work and I have no idea of a solution. Does anybody can help me please?

  it('Verify if the function argument is a number', () => {
     const myMock = jest.fn(2021);
     myMock.mock.calls[0].toBe(expect.any(Number));
  });
Kelson Batista
  • 406
  • 4
  • 25
  • Can you work in `!isNaN(arg)`? – Abe Nov 14 '21 at 18:48
  • How the Jest use this @Abe? – Kelson Batista Nov 14 '21 at 18:50
  • I'm not sure what you're testing exactly. I don't know why Jest is being used - Jest is dev-facing, so it won't tell you what the user does with your code. If you want to know what the user does in production, you could modify your function to be something like this: `const isLeapYear = year => { if isNaN(parseInt(year)) throw new Error('year must be a number'); return ; };` – Abe Nov 14 '21 at 18:55
  • I got your point. Actually I'd like to test it with Jest, not touching the function. So basically it's not possible to directly test the argument with Jest in test file? – Kelson Batista Nov 14 '21 at 19:05

0 Answers0