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

113
Views
Test array of objects for values equal to values from another array in Chai

I'm totally new to Chai, and this is probably really simple, but I can't find any reference to my case in the official docs. I'm trying to verify that an array with two objects has a property value exactly matching values in another array.

Consider the following:

test('Array includes ids', function() {
    const array1 = [
        {
            type: 'type1',
            id: '1'
        },
        {
            type: 'type2',
            id: '2'      
        },
    ]
        
    const array2 = ['1', '2']
    
    chai.expect(array1).to.have.deep.members(array2) // fails
})

I'd like to verify that objects in array1 has an id property with one of the values found in array2. I know I could map array1 as array1.map(p => p.id) and compare that with array2, but that seems to go against testing as a practice. After all, I don't want to tamper too much with my target to make it conform to my current level of knowledge.

Is there a better way to test this? (Needless to say, my example code above does not work as desired.)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just transform your array1 with .map() method to have your kind of array with only ids in it just like array2,

test('Array includes ids', function() {
    const array1 = [
        {
            type: 'type1',
            id: '1'
        },
        {
            type: 'type2',
            id: '2'      
        },
    ]
        
    const arr1 = array1.map(e=>e.id);//transform with .map() -> ['1', '2']
    const array2 = ['1', '2']
    
    chai.expect(arr1).to.have.deep.members(array2) // pass
})
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!