mercredi 12 août 2020

Can there be more abstract unit test cases in Jest?

I recently started learning unit testing with Jest. I started with the classic FizzBuzz problem. The tutorials I'm seeing, all of them test for specific inputs.

i.e. :

it('returns Fizz for multiples of 3', () => {
expect(fizzbuzz(6)).toBe('Fizz');
expect(fizzbuzz(9)).toBe('Fizz');
}) 

And after these 2-3 expect statements, they declare the 'test case handled'.

Question is: Could we write something like this below, for a more abstract approach?

it('returns Fizz for multiples of 3', (num) => {
if (num % 3 === 0){
expect(fizzbuzz(num)).toBe('Fizz');
}
})

I tried it and it gives me some errors. It's either myself or this is not a good approach.

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire