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

188
Vistas
How to test if an array contains any value from another array using Jest?

I have an array:

   Const domesticAnimals = ['Chicken','lama','Donkey']
   Const wildAnimals =['lama','lion','elephant']

How do I test If any wildAnimals is listed in domesticAnimals array?

I have tried

test('test If any wild animal is in the domestic animal list', async() =>{
expect(domesticAnimals ).toContain('lama')
}) //works perfectly

test('test If any wild animal is in the domestic animal list', async() =>{
expect(domesticAnimals ).toContain(wildAnimals) // does not work
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You actually have TWO arrays:

const domesticAnimals = ['Chicken','lama','Donkey'];
const wildAnimals =['lama','lion','elephant'];

The plain JS way of testing if one array contains any values from the other would be:

domesticAnimals.some(domesticAnimal => wildAnimals.includes(domesticAnimal));

So to put this in a test, would be:

test('test If any wild animal is in the domestic animal list', () =>{
    expect(domesticAnimals.some(
        domesticAnimal => wildAnimals.includes(domesticAnimal)
    )).toBe(true);
});

As a side-note, you marked the test functions as async but don't actually await anything, so the async is redundant.

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