Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!