Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

341
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda