vendredi 30 septembre 2022

How can I test a function that takes an array and outputs 3 random elements?

I want to test this function using Jest. The problem is that I don't know how I can write the test without adding all the possible outcomes in the toBe(). Any hints are welcomed.

const options = [1, 2, 3, 4]
 const getElements= (options) => {
  const getRandomEl = (list) => list[Math.floor(Math.random() * list.length)];
  return [
    getRandomEl(options),
    getRandomEl(options),
    getRandomEl(options),
  ];

And here is my test:

describe("Helper tests", () => {
    test('We should get 3 random elements from a list', () => {
      expect(mathOperations.getElements([1, 2, 3, 4])).toBe([1, 2, 3] | [2, 3, 4] | [1, 2, 4]); //how to avoid adding all possible combinations in the toBe()?
    });
   })
};




Aucun commentaire:

Enregistrer un commentaire