Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
Why is my jest test of a simple boolean statement taking much longer than it does console?

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?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda