Skip to content
Advertisement

Looping through an array and run a Jest test for each element does not work

I have a very large array of JSON objects. I need to run Jest tests on each individual element. I tried iterating through the array first and then write the tests in the loop as such:

JavaScript

However with this code Jest complains that ” Your test suite must contain at least one test.”

Do I have to loop over this array for every single test I have?

Advertisement

Answer

Jest does have describe.each, test.each and it.each methods for your needs. It allows you to make same tests with different input/output.

https://jestjs.io/docs/api#describeeachtablename-fn-timeout

Examples :

With global describe.each :

JavaScript

Output :

JavaScript

Or with simple it.each :

JavaScript

Output :

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement