I have a function - let's call it generateMove that returns a random value on a board of 10 x 10. There are 100 possible values. generateMove should check that its random move isn't taken before returning its chosen move.
In console it is very easy for me to test this by calling the function 99 times and making an array of all the moves.
ie.
const alreadyTaken = [];
for (let i=0;i<99;i++) {
alreadyTaken.push(generateMove());
}
Then I can take one final move by calling generateMove again and assigning it to a variable, ie. const last = generateMove();
To check if the last move is included in the array of the other 99 possible values I use const isIncluded = alreadyTaken.includes(last) which if I console log it correctly logs false
I am trying to do this exact same thing in a Jest test function ie.
test('ai does not hit places that were already hit', () => {
const alreadyPlayed = [];
for (let i=0;i<99;i++) {
alreadyPlayed.push(generateMove());
}
const last = generateMove();
const isIncluded = alreadyPlayed.includes(last);
expect(isIncluded).toEqual(false);
})
which should be fairly simple right? all of the heavy lifting has been done and the variable isIncluded should be a simple boolean falsewhich is the only value matching Jest has to accomplish but it keeps crashing and timing out on test.
I've tried putting the loop and other code outside of the 'test' function and it doesn't help - all of the code is inside a describe bloc however if that is relevant.
Does anyone know why Jest is crashing? Can it not handle for-loops etc. within test suites?