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

332
Visualizações
How to check for multiple values in JEST? (OR-operator)

how do I check if functions returns one of multiple possible values?

My current test function:

const { randomTense } = require('./src/models/functions');

test('Category exists', () => {
    const data = randomTense()
    console.log(data.category)
    expect(data.category).toMatch('past' || 'present' || 'future' );
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use the 'toContain' method like below.

Note that this is not a good test as it's not exhaustive or deterministic. This may fail randomly if your data.category is sometimes something else. Imagine that the function randomTense returns four strings in random order: past, present, future and this is a bug. In that case this test will pass three times out of four and because it's random it's impossible (it's harder) to predict when it fails.

Testing random functions isn't really a thing. What you usually do in these cases is to separate the function out into smaller bits and mock the random part.

So instead of having a function that does all the things plus the random selection you take all the logic out of it and only leave the random selection which is usually only a line. Then you mock that functionality for your test and test the other function that returns the options/enums.

const { randomTense } = require('./src/models/functions');

const POSSIBLE_VALUES = ['past', 'present', 'future'];

test('Category exists', () => {
    const data = randomTense();
    expect(POSSIBLE_VALUES).toContain(data.category);
});
about 4 years ago · Juan Pablo Isaza Relatório
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