Categories
JavaScript Answers

How to loop through dynamic test cases with Jest and JavaScript?

Spread the love

To loop through dynamic test cases with Jest and JavaScript, we use the each method.

For instance, we write

test.each([
  [1, 1, 2],
  [1, 2, 3],
  [2, 1, 3],
])(".add(%i, %i)", (a, b, expected) => {
  expect(a + b).toBe(expected);
});

to call test.each with an array of arguments for the callback.

In the callback, we add the test code that uses the parameters.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *