Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jest testing a loop with Booleans

I have the following function in Jest. I have a method called isAquatic that returns a bool depending on the animal.

        const nonAquaticAnimal = ["tiger", "cat", "lion"]
        test.each(nonAquaticAnimal)(
          '.isAquatic',
          (input, false) => {
            const animal = isAquatic(input)
            expect(animal).toBe(false);
          },
        );

I have an error that says identifier false is a reserved word and cannot be used here. How can I loop through the array and call this method which returns a boolean?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The array argument passed to test.each is usually a 2D array, so you can have the animal and the expected result for that animal in the nonAquaticAnimals array. And the test function would both the items in as function arguments.

You can have your test as follows:

const nonAquaticAnimals = [
  ["tiger", false],
  ["cat", false],
  ["lion", false],
];
test.each(nonAquaticAnimals)(".isAquatic", (animal, expected) => {
  expect(isAquatic(animal)).toBe(expected);
});

You can also omit the expected value from your array if you find it redundant:

const nonAquaticAnimals = ["tiger", "cat", "lion"];
test.each(nonAquaticAnimals)(".isAquatic", (animal) => {
  expect(isAquatic(animal)).toBe(false);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading