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

184
Visualizações
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 Respostas
Responde à pergunta

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 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